keywest.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * common keywest i2c layer
  4. *
  5. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/i2c.h>
  9. #include <linux/delay.h>
  10. #include <linux/module.h>
  11. #include <sound/core.h>
  12. #include "pmac.h"
  13. static struct pmac_keywest *keywest_ctx;
  14. static bool keywest_probed;
  15. static int keywest_probe(struct i2c_client *client)
  16. {
  17. keywest_probed = true;
  18. /* If instantiated via i2c-powermac, we still need to set the client */
  19. if (!keywest_ctx->client)
  20. keywest_ctx->client = client;
  21. i2c_set_clientdata(client, keywest_ctx);
  22. return 0;
  23. }
  24. /*
  25. * This is kind of a hack, best would be to turn powermac to fixed i2c
  26. * bus numbers and declare the sound device as part of platform
  27. * initialization
  28. */
  29. static int keywest_attach_adapter(struct i2c_adapter *adapter)
  30. {
  31. struct i2c_board_info info;
  32. struct i2c_client *client;
  33. if (! keywest_ctx)
  34. return -EINVAL;
  35. if (strncmp(adapter->name, "mac-io", 6))
  36. return -EINVAL; /* ignored */
  37. memset(&info, 0, sizeof(struct i2c_board_info));
  38. strscpy(info.type, "keywest", I2C_NAME_SIZE);
  39. info.addr = keywest_ctx->addr;
  40. client = i2c_new_client_device(adapter, &info);
  41. if (IS_ERR(client))
  42. return PTR_ERR(client);
  43. keywest_ctx->client = client;
  44. /*
  45. * We know the driver is already loaded, so the device should be
  46. * already bound. If not it means binding failed, and then there
  47. * is no point in keeping the device instantiated.
  48. */
  49. if (!keywest_ctx->client->dev.driver) {
  50. i2c_unregister_device(keywest_ctx->client);
  51. keywest_ctx->client = NULL;
  52. return -ENODEV;
  53. }
  54. /*
  55. * Let i2c-core delete that device on driver removal.
  56. * This is safe because i2c-core holds the core_lock mutex for us.
  57. */
  58. list_add_tail(&keywest_ctx->client->detected,
  59. &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
  60. return 0;
  61. }
  62. static void keywest_remove(struct i2c_client *client)
  63. {
  64. if (! keywest_ctx)
  65. return;
  66. if (client == keywest_ctx->client)
  67. keywest_ctx->client = NULL;
  68. }
  69. static const struct i2c_device_id keywest_i2c_id[] = {
  70. { "MAC,tas3004" }, /* instantiated by i2c-powermac */
  71. { "keywest" }, /* instantiated by us if needed */
  72. { }
  73. };
  74. MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
  75. static struct i2c_driver keywest_driver = {
  76. .driver = {
  77. .name = "PMac Keywest Audio",
  78. },
  79. .probe = keywest_probe,
  80. .remove = keywest_remove,
  81. .id_table = keywest_i2c_id,
  82. };
  83. /* exported */
  84. void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
  85. {
  86. if (keywest_ctx && keywest_ctx == i2c) {
  87. i2c_del_driver(&keywest_driver);
  88. keywest_ctx = NULL;
  89. }
  90. }
  91. int snd_pmac_tumbler_post_init(void)
  92. {
  93. int err;
  94. if (!keywest_ctx || !keywest_ctx->client)
  95. return -ENXIO;
  96. err = keywest_ctx->init_client(keywest_ctx);
  97. if (err < 0) {
  98. dev_err(&keywest_ctx->client->dev,
  99. "tumbler: %i :cannot initialize the MCS\n", err);
  100. return err;
  101. }
  102. return 0;
  103. }
  104. /* exported */
  105. int snd_pmac_keywest_init(struct pmac_keywest *i2c)
  106. {
  107. struct i2c_adapter *adap;
  108. int err, i = 0;
  109. if (keywest_ctx)
  110. return -EBUSY;
  111. adap = i2c_get_adapter(0);
  112. if (!adap)
  113. return -EPROBE_DEFER;
  114. keywest_ctx = i2c;
  115. err = i2c_add_driver(&keywest_driver);
  116. if (err) {
  117. dev_err(&i2c->client->dev, "cannot register keywest i2c driver\n");
  118. i2c_put_adapter(adap);
  119. return err;
  120. }
  121. /* There was already a device from i2c-powermac. Great, let's return */
  122. if (keywest_probed)
  123. return 0;
  124. /* We assume Macs have consecutive I2C bus numbers starting at 0 */
  125. while (adap) {
  126. /* Scan for devices to be bound to */
  127. err = keywest_attach_adapter(adap);
  128. if (!err)
  129. return 0;
  130. i2c_put_adapter(adap);
  131. adap = i2c_get_adapter(++i);
  132. }
  133. return -ENODEV;
  134. }