keywest.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 pmac_keywest_t *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. .name = "PMac Keywest Audio",
  38. .id = I2C_DRIVERID_KEYWEST,
  39. .flags = I2C_DF_NOTIFY,
  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 = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  55. if (! new_client)
  56. return -ENOMEM;
  57. memset(new_client, 0, sizeof(*new_client));
  58. new_client->addr = keywest_ctx->addr;
  59. i2c_set_clientdata(new_client, keywest_ctx);
  60. new_client->adapter = adapter;
  61. new_client->driver = &keywest_driver;
  62. new_client->flags = 0;
  63. strcpy(i2c_device_name(new_client), keywest_ctx->name);
  64. keywest_ctx->client = new_client;
  65. /* Tell the i2c layer a new client has arrived */
  66. if (i2c_attach_client(new_client)) {
  67. snd_printk(KERN_ERR "tumbler: cannot attach i2c client\n");
  68. err = -ENODEV;
  69. goto __err;
  70. }
  71. return 0;
  72. __err:
  73. kfree(new_client);
  74. keywest_ctx->client = NULL;
  75. return err;
  76. }
  77. static int keywest_detach_client(struct i2c_client *client)
  78. {
  79. if (! keywest_ctx)
  80. return 0;
  81. if (client == keywest_ctx->client)
  82. keywest_ctx->client = NULL;
  83. i2c_detach_client(client);
  84. kfree(client);
  85. return 0;
  86. }
  87. /* exported */
  88. void snd_pmac_keywest_cleanup(pmac_keywest_t *i2c)
  89. {
  90. if (keywest_ctx && keywest_ctx == i2c) {
  91. i2c_del_driver(&keywest_driver);
  92. keywest_ctx = NULL;
  93. }
  94. }
  95. int __init snd_pmac_tumbler_post_init(void)
  96. {
  97. int err;
  98. if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
  99. snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
  100. return err;
  101. }
  102. return 0;
  103. }
  104. /* exported */
  105. int __init snd_pmac_keywest_init(pmac_keywest_t *i2c)
  106. {
  107. int err;
  108. if (keywest_ctx)
  109. return -EBUSY;
  110. keywest_ctx = i2c;
  111. if ((err = i2c_add_driver(&keywest_driver))) {
  112. snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
  113. return err;
  114. }
  115. return 0;
  116. }