wm8400-core.c 4.4 KB

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