wm8400-core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Core driver for WM8400.
  4. *
  5. * Copyright 2008 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/bug.h>
  11. #include <linux/err.h>
  12. #include <linux/i2c.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/mfd/wm8400-private.h>
  16. #include <linux/mfd/wm8400-audio.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. static bool wm8400_volatile(struct device *dev, unsigned int reg)
  20. {
  21. switch (reg) {
  22. case WM8400_INTERRUPT_STATUS_1:
  23. case WM8400_INTERRUPT_LEVELS:
  24. case WM8400_SHUTDOWN_REASON:
  25. return true;
  26. default:
  27. return false;
  28. }
  29. }
  30. static int wm8400_register_codec(struct wm8400 *wm8400)
  31. {
  32. const struct mfd_cell cell = {
  33. .name = "wm8400-codec",
  34. .platform_data = wm8400,
  35. .pdata_size = sizeof(*wm8400),
  36. };
  37. return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
  38. }
  39. /*
  40. * wm8400_init - Generic initialisation
  41. *
  42. * The WM8400 can be configured as either an I2C or SPI device. Probe
  43. * functions for each bus set up the accessors then call into this to
  44. * set up the device itself.
  45. */
  46. static int wm8400_init(struct wm8400 *wm8400,
  47. struct wm8400_platform_data *pdata)
  48. {
  49. unsigned int reg;
  50. int ret;
  51. dev_set_drvdata(wm8400->dev, wm8400);
  52. /* Check that this is actually a WM8400 */
  53. ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
  54. if (ret != 0) {
  55. dev_err(wm8400->dev, "Chip ID register read failed\n");
  56. return -EIO;
  57. }
  58. if (reg != 0x6172) {
  59. dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
  60. reg);
  61. return -ENODEV;
  62. }
  63. ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
  64. if (ret != 0) {
  65. dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
  66. return ret;
  67. }
  68. reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
  69. dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
  70. ret = wm8400_register_codec(wm8400);
  71. if (ret != 0) {
  72. dev_err(wm8400->dev, "Failed to register codec\n");
  73. return ret;
  74. }
  75. if (pdata && pdata->platform_init) {
  76. ret = pdata->platform_init(wm8400->dev);
  77. if (ret != 0) {
  78. dev_err(wm8400->dev, "Platform init failed: %d\n",
  79. ret);
  80. return ret;
  81. }
  82. } else
  83. dev_warn(wm8400->dev, "No platform initialisation supplied\n");
  84. return 0;
  85. }
  86. static const struct regmap_config wm8400_regmap_config = {
  87. .reg_bits = 8,
  88. .val_bits = 16,
  89. .max_register = WM8400_REGISTER_COUNT - 1,
  90. .volatile_reg = wm8400_volatile,
  91. .cache_type = REGCACHE_RBTREE,
  92. };
  93. /**
  94. * wm8400_reset_codec_reg_cache - Reset cached codec registers to
  95. * their default values.
  96. *
  97. * @wm8400: pointer to local driver data structure
  98. */
  99. void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
  100. {
  101. regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
  102. }
  103. EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
  104. #if IS_ENABLED(CONFIG_I2C)
  105. static int wm8400_i2c_probe(struct i2c_client *i2c,
  106. const struct i2c_device_id *id)
  107. {
  108. struct wm8400 *wm8400;
  109. wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
  110. if (!wm8400)
  111. return -ENOMEM;
  112. wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
  113. if (IS_ERR(wm8400->regmap))
  114. return PTR_ERR(wm8400->regmap);
  115. wm8400->dev = &i2c->dev;
  116. i2c_set_clientdata(i2c, wm8400);
  117. return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
  118. }
  119. static const struct i2c_device_id wm8400_i2c_id[] = {
  120. { "wm8400", 0 },
  121. { }
  122. };
  123. static struct i2c_driver wm8400_i2c_driver = {
  124. .driver = {
  125. .name = "WM8400",
  126. },
  127. .probe = wm8400_i2c_probe,
  128. .id_table = wm8400_i2c_id,
  129. };
  130. #endif
  131. static int __init wm8400_driver_init(void)
  132. {
  133. int ret = -ENODEV;
  134. #if IS_ENABLED(CONFIG_I2C)
  135. ret = i2c_add_driver(&wm8400_i2c_driver);
  136. if (ret != 0)
  137. pr_err("Failed to register I2C driver: %d\n", ret);
  138. #endif
  139. return ret;
  140. }
  141. subsys_initcall(wm8400_driver_init);