wm831x-spi.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * wm831x-spi.c -- SPI access for Wolfson WM831x PMICs
  4. *
  5. * Copyright 2009,2010 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pm.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/regmap.h>
  16. #include <linux/err.h>
  17. #include <linux/mfd/wm831x/core.h>
  18. static int wm831x_spi_probe(struct spi_device *spi)
  19. {
  20. struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev);
  21. const struct spi_device_id *id = spi_get_device_id(spi);
  22. const struct of_device_id *of_id;
  23. struct wm831x *wm831x;
  24. enum wm831x_parent type;
  25. int ret;
  26. if (spi->dev.of_node) {
  27. of_id = of_match_device(wm831x_of_match, &spi->dev);
  28. if (!of_id) {
  29. dev_err(&spi->dev, "Failed to match device\n");
  30. return -ENODEV;
  31. }
  32. type = (enum wm831x_parent)of_id->data;
  33. } else {
  34. type = (enum wm831x_parent)id->driver_data;
  35. }
  36. wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
  37. if (wm831x == NULL)
  38. return -ENOMEM;
  39. spi->mode = SPI_MODE_0;
  40. spi_set_drvdata(spi, wm831x);
  41. wm831x->dev = &spi->dev;
  42. wm831x->type = type;
  43. wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
  44. if (IS_ERR(wm831x->regmap)) {
  45. ret = PTR_ERR(wm831x->regmap);
  46. dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
  47. ret);
  48. return ret;
  49. }
  50. if (pdata)
  51. memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
  52. return wm831x_device_init(wm831x, spi->irq);
  53. }
  54. static int wm831x_spi_suspend(struct device *dev)
  55. {
  56. struct wm831x *wm831x = dev_get_drvdata(dev);
  57. return wm831x_device_suspend(wm831x);
  58. }
  59. static int wm831x_spi_poweroff(struct device *dev)
  60. {
  61. struct wm831x *wm831x = dev_get_drvdata(dev);
  62. wm831x_device_shutdown(wm831x);
  63. return 0;
  64. }
  65. static const struct dev_pm_ops wm831x_spi_pm = {
  66. .freeze = wm831x_spi_suspend,
  67. .suspend = wm831x_spi_suspend,
  68. .poweroff = wm831x_spi_poweroff,
  69. };
  70. static const struct spi_device_id wm831x_spi_ids[] = {
  71. { "wm8310", WM8310 },
  72. { "wm8311", WM8311 },
  73. { "wm8312", WM8312 },
  74. { "wm8320", WM8320 },
  75. { "wm8321", WM8321 },
  76. { "wm8325", WM8325 },
  77. { "wm8326", WM8326 },
  78. { },
  79. };
  80. static struct spi_driver wm831x_spi_driver = {
  81. .driver = {
  82. .name = "wm831x",
  83. .pm = &wm831x_spi_pm,
  84. .of_match_table = of_match_ptr(wm831x_of_match),
  85. .suppress_bind_attrs = true,
  86. },
  87. .id_table = wm831x_spi_ids,
  88. .probe = wm831x_spi_probe,
  89. };
  90. static int __init wm831x_spi_init(void)
  91. {
  92. int ret;
  93. ret = spi_register_driver(&wm831x_spi_driver);
  94. if (ret != 0)
  95. pr_err("Failed to register WM831x SPI driver: %d\n", ret);
  96. return 0;
  97. }
  98. subsys_initcall(wm831x_spi_init);