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 <[email protected]>
  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. const struct i2c_device_id *id)
  17. {
  18. keywest_probed = true;
  19. /* If instantiated via i2c-powermac, we still need to set the client */
  20. if (!keywest_ctx->client)
  21. keywest_ctx->client = client;
  22. i2c_set_clientdata(client, keywest_ctx);
  23. return 0;
  24. }
  25. /*
  26. * This is kind of a hack, best would be to turn powermac to fixed i2c
  27. * bus numbers and declare the sound device as part of platform
  28. * initialization
  29. */
  30. static int keywest_attach_adapter(struct i2c_adapter *adapter)
  31. {
  32. struct i2c_board_info info;
  33. struct i2c_client *client;
  34. if (! keywest_ctx)
  35. return -EINVAL;
  36. if (strncmp(adapter->name, "mac-io", 6))
  37. return -EINVAL; /* ignored */
  38. memset(&info, 0, sizeof(struct i2c_board_info));
  39. strscpy(info.type, "keywest", I2C_NAME_SIZE);
  40. info.addr = keywest_ctx->addr;
  41. client = i2c_new_client_device(adapter, &info);
  42. if (IS_ERR(client))
  43. return PTR_ERR(client);
  44. keywest_ctx->client = client;
  45. /*
  46. * We know the driver is already loaded, so the device should be
  47. * already bound. If not it means binding failed, and then there
  48. * is no point in keeping the device instantiated.
  49. */
  50. if (!keywest_ctx->client->dev.driver) {
  51. i2c_unregister_device(keywest_ctx->client);
  52. keywest_ctx->client = NULL;
  53. return -ENODEV;
  54. }
  55. /*
  56. * Let i2c-core delete that device on driver removal.
  57. * This is safe because i2c-core holds the core_lock mutex for us.
  58. */
  59. list_add_tail(&keywest_ctx->client->detected,
  60. &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
  61. return 0;
  62. }
  63. static void keywest_remove(struct i2c_client *client)
  64. {
  65. if (! keywest_ctx)
  66. return;
  67. if (client == keywest_ctx->client)
  68. keywest_ctx->client = NULL;
  69. }
  70. static const struct i2c_device_id keywest_i2c_id[] = {
  71. { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */
  72. { "keywest", 0 }, /* instantiated by us if needed */
  73. { }
  74. };
  75. MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
  76. static struct i2c_driver keywest_driver = {
  77. .driver = {
  78. .name = "PMac Keywest Audio",
  79. },
  80. .probe = keywest_probe,
  81. .remove = keywest_remove,
  82. .id_table = keywest_i2c_id,
  83. };
  84. /* exported */
  85. void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
  86. {
  87. if (keywest_ctx && keywest_ctx == i2c) {
  88. i2c_del_driver(&keywest_driver);
  89. keywest_ctx = NULL;
  90. }
  91. }
  92. int snd_pmac_tumbler_post_init(void)
  93. {
  94. int err;
  95. if (!keywest_ctx || !keywest_ctx->client)
  96. return -ENXIO;
  97. err = keywest_ctx->init_client(keywest_ctx);
  98. if (err < 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 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. snd_printk(KERN_ERR "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. }