wm831x-spi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * wm831x-spi.c -- SPI access for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009,2010 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 modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pm.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/regmap.h>
  21. #include <linux/err.h>
  22. #include <linux/mfd/wm831x/core.h>
  23. static int wm831x_spi_probe(struct spi_device *spi)
  24. {
  25. struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev);
  26. const struct spi_device_id *id = spi_get_device_id(spi);
  27. const struct of_device_id *of_id;
  28. struct wm831x *wm831x;
  29. enum wm831x_parent type;
  30. int ret;
  31. if (spi->dev.of_node) {
  32. of_id = of_match_device(wm831x_of_match, &spi->dev);
  33. if (!of_id) {
  34. dev_err(&spi->dev, "Failed to match device\n");
  35. return -ENODEV;
  36. }
  37. type = (enum wm831x_parent)of_id->data;
  38. } else {
  39. type = (enum wm831x_parent)id->driver_data;
  40. }
  41. wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
  42. if (wm831x == NULL)
  43. return -ENOMEM;
  44. spi->mode = SPI_MODE_0;
  45. spi_set_drvdata(spi, wm831x);
  46. wm831x->dev = &spi->dev;
  47. wm831x->type = type;
  48. wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
  49. if (IS_ERR(wm831x->regmap)) {
  50. ret = PTR_ERR(wm831x->regmap);
  51. dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
  52. ret);
  53. return ret;
  54. }
  55. if (pdata)
  56. memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
  57. return wm831x_device_init(wm831x, spi->irq);
  58. }
  59. static int wm831x_spi_remove(struct spi_device *spi)
  60. {
  61. struct wm831x *wm831x = spi_get_drvdata(spi);
  62. wm831x_device_exit(wm831x);
  63. return 0;
  64. }
  65. static int wm831x_spi_suspend(struct device *dev)
  66. {
  67. struct wm831x *wm831x = dev_get_drvdata(dev);
  68. return wm831x_device_suspend(wm831x);
  69. }
  70. static int wm831x_spi_poweroff(struct device *dev)
  71. {
  72. struct wm831x *wm831x = dev_get_drvdata(dev);
  73. wm831x_device_shutdown(wm831x);
  74. return 0;
  75. }
  76. static const struct dev_pm_ops wm831x_spi_pm = {
  77. .freeze = wm831x_spi_suspend,
  78. .suspend = wm831x_spi_suspend,
  79. .poweroff = wm831x_spi_poweroff,
  80. };
  81. static const struct spi_device_id wm831x_spi_ids[] = {
  82. { "wm8310", WM8310 },
  83. { "wm8311", WM8311 },
  84. { "wm8312", WM8312 },
  85. { "wm8320", WM8320 },
  86. { "wm8321", WM8321 },
  87. { "wm8325", WM8325 },
  88. { "wm8326", WM8326 },
  89. { },
  90. };
  91. MODULE_DEVICE_TABLE(spi, wm831x_spi_ids);
  92. static struct spi_driver wm831x_spi_driver = {
  93. .driver = {
  94. .name = "wm831x",
  95. .pm = &wm831x_spi_pm,
  96. .of_match_table = of_match_ptr(wm831x_of_match),
  97. },
  98. .id_table = wm831x_spi_ids,
  99. .probe = wm831x_spi_probe,
  100. .remove = wm831x_spi_remove,
  101. };
  102. static int __init wm831x_spi_init(void)
  103. {
  104. int ret;
  105. ret = spi_register_driver(&wm831x_spi_driver);
  106. if (ret != 0)
  107. pr_err("Failed to register WM831x SPI driver: %d\n", ret);
  108. return 0;
  109. }
  110. subsys_initcall(wm831x_spi_init);
  111. static void __exit wm831x_spi_exit(void)
  112. {
  113. spi_unregister_driver(&wm831x_spi_driver);
  114. }
  115. module_exit(wm831x_spi_exit);
  116. MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
  117. MODULE_LICENSE("GPL");
  118. MODULE_AUTHOR("Mark Brown");