wm8400-core.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  36. * wm8400_reg_read - Single register read
  37. *
  38. * @wm8400: Pointer to wm8400 control structure
  39. * @reg: Register to read
  40. *
  41. * @return Read value
  42. */
  43. u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg)
  44. {
  45. unsigned int val;
  46. int ret;
  47. ret = regmap_read(wm8400->regmap, reg, &val);
  48. if (ret < 0)
  49. return ret;
  50. return val;
  51. }
  52. EXPORT_SYMBOL_GPL(wm8400_reg_read);
  53. int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
  54. {
  55. return regmap_bulk_read(wm8400->regmap, reg, data, count);
  56. }
  57. EXPORT_SYMBOL_GPL(wm8400_block_read);
  58. static int wm8400_register_codec(struct wm8400 *wm8400)
  59. {
  60. const struct mfd_cell cell = {
  61. .name = "wm8400-codec",
  62. .platform_data = wm8400,
  63. .pdata_size = sizeof(*wm8400),
  64. };
  65. return mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
  66. }
  67. /*
  68. * wm8400_init - Generic initialisation
  69. *
  70. * The WM8400 can be configured as either an I2C or SPI device. Probe
  71. * functions for each bus set up the accessors then call into this to
  72. * set up the device itself.
  73. */
  74. static int wm8400_init(struct wm8400 *wm8400,
  75. struct wm8400_platform_data *pdata)
  76. {
  77. unsigned int reg;
  78. int ret;
  79. dev_set_drvdata(wm8400->dev, wm8400);
  80. /* Check that this is actually a WM8400 */
  81. ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
  82. if (ret != 0) {
  83. dev_err(wm8400->dev, "Chip ID register read failed\n");
  84. return -EIO;
  85. }
  86. if (reg != 0x6172) {
  87. dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
  88. reg);
  89. return -ENODEV;
  90. }
  91. ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
  92. if (ret != 0) {
  93. dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
  94. return ret;
  95. }
  96. reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
  97. dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
  98. ret = wm8400_register_codec(wm8400);
  99. if (ret != 0) {
  100. dev_err(wm8400->dev, "Failed to register codec\n");
  101. goto err_children;
  102. }
  103. if (pdata && pdata->platform_init) {
  104. ret = pdata->platform_init(wm8400->dev);
  105. if (ret != 0) {
  106. dev_err(wm8400->dev, "Platform init failed: %d\n",
  107. ret);
  108. goto err_children;
  109. }
  110. } else
  111. dev_warn(wm8400->dev, "No platform initialisation supplied\n");
  112. return 0;
  113. err_children:
  114. mfd_remove_devices(wm8400->dev);
  115. return ret;
  116. }
  117. static void wm8400_release(struct wm8400 *wm8400)
  118. {
  119. mfd_remove_devices(wm8400->dev);
  120. }
  121. static const struct regmap_config wm8400_regmap_config = {
  122. .reg_bits = 8,
  123. .val_bits = 16,
  124. .max_register = WM8400_REGISTER_COUNT - 1,
  125. .volatile_reg = wm8400_volatile,
  126. .cache_type = REGCACHE_RBTREE,
  127. };
  128. /**
  129. * wm8400_reset_codec_reg_cache - Reset cached codec registers to
  130. * their default values.
  131. */
  132. void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
  133. {
  134. regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
  135. }
  136. EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
  137. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  138. static int wm8400_i2c_probe(struct i2c_client *i2c,
  139. const struct i2c_device_id *id)
  140. {
  141. struct wm8400 *wm8400;
  142. wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
  143. if (!wm8400)
  144. return -ENOMEM;
  145. wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
  146. if (IS_ERR(wm8400->regmap))
  147. return PTR_ERR(wm8400->regmap);
  148. wm8400->dev = &i2c->dev;
  149. i2c_set_clientdata(i2c, wm8400);
  150. return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
  151. }
  152. static int wm8400_i2c_remove(struct i2c_client *i2c)
  153. {
  154. struct wm8400 *wm8400 = i2c_get_clientdata(i2c);
  155. wm8400_release(wm8400);
  156. return 0;
  157. }
  158. static const struct i2c_device_id wm8400_i2c_id[] = {
  159. { "wm8400", 0 },
  160. { }
  161. };
  162. MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id);
  163. static struct i2c_driver wm8400_i2c_driver = {
  164. .driver = {
  165. .name = "WM8400",
  166. },
  167. .probe = wm8400_i2c_probe,
  168. .remove = wm8400_i2c_remove,
  169. .id_table = wm8400_i2c_id,
  170. };
  171. #endif
  172. static int __init wm8400_module_init(void)
  173. {
  174. int ret = -ENODEV;
  175. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  176. ret = i2c_add_driver(&wm8400_i2c_driver);
  177. if (ret != 0)
  178. pr_err("Failed to register I2C driver: %d\n", ret);
  179. #endif
  180. return ret;
  181. }
  182. subsys_initcall(wm8400_module_init);
  183. static void __exit wm8400_module_exit(void)
  184. {
  185. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  186. i2c_del_driver(&wm8400_i2c_driver);
  187. #endif
  188. }
  189. module_exit(wm8400_module_exit);
  190. MODULE_LICENSE("GPL");
  191. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");