keywest.c 3.4 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/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. .owner = THIS_MODULE,
  39. .name = "PMac Keywest Audio",
  40. },
  41. .id = I2C_DRIVERID_KEYWEST,
  42. .attach_adapter = &keywest_attach_adapter,
  43. .detach_client = &keywest_detach_client,
  44. };
  45. #ifndef i2c_device_name
  46. #define i2c_device_name(x) ((x)->name)
  47. #endif
  48. static int keywest_attach_adapter(struct i2c_adapter *adapter)
  49. {
  50. int err;
  51. struct i2c_client *new_client;
  52. if (! keywest_ctx)
  53. return -EINVAL;
  54. if (strncmp(i2c_device_name(adapter), "mac-io", 6))
  55. return 0; /* ignored */
  56. new_client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  57. if (! new_client)
  58. return -ENOMEM;
  59. memset(new_client, 0, sizeof(*new_client));
  60. new_client->addr = keywest_ctx->addr;
  61. i2c_set_clientdata(new_client, keywest_ctx);
  62. new_client->adapter = adapter;
  63. new_client->driver = &keywest_driver;
  64. new_client->flags = 0;
  65. strcpy(i2c_device_name(new_client), keywest_ctx->name);
  66. keywest_ctx->client = new_client;
  67. /* Tell the i2c layer a new client has arrived */
  68. if (i2c_attach_client(new_client)) {
  69. snd_printk(KERN_ERR "tumbler: cannot attach i2c client\n");
  70. err = -ENODEV;
  71. goto __err;
  72. }
  73. return 0;
  74. __err:
  75. kfree(new_client);
  76. keywest_ctx->client = NULL;
  77. return err;
  78. }
  79. static int keywest_detach_client(struct i2c_client *client)
  80. {
  81. if (! keywest_ctx)
  82. return 0;
  83. if (client == keywest_ctx->client)
  84. keywest_ctx->client = NULL;
  85. i2c_detach_client(client);
  86. kfree(client);
  87. return 0;
  88. }
  89. /* exported */
  90. void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
  91. {
  92. if (keywest_ctx && keywest_ctx == i2c) {
  93. i2c_del_driver(&keywest_driver);
  94. keywest_ctx = NULL;
  95. }
  96. }
  97. int __init snd_pmac_tumbler_post_init(void)
  98. {
  99. int err;
  100. if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
  101. snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
  102. return err;
  103. }
  104. return 0;
  105. }
  106. /* exported */
  107. int __init snd_pmac_keywest_init(struct pmac_keywest *i2c)
  108. {
  109. int err;
  110. if (keywest_ctx)
  111. return -EBUSY;
  112. keywest_ctx = i2c;
  113. if ((err = i2c_add_driver(&keywest_driver))) {
  114. snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
  115. return err;
  116. }
  117. return 0;
  118. }