zpa2326_spi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Murata ZPA2326 SPI pressure and temperature sensor driver
  3. *
  4. * Copyright (c) 2016 Parrot S.A.
  5. *
  6. * Author: Gregor Boirie <gregor.boirie@parrot.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 version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/regmap.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/of_device.h>
  21. #include "zpa2326.h"
  22. /*
  23. * read_flag_mask:
  24. * - address bit 7 must be set to request a register read operation
  25. * - address bit 6 must be set to request register address auto increment
  26. */
  27. static const struct regmap_config zpa2326_regmap_spi_config = {
  28. .reg_bits = 8,
  29. .val_bits = 8,
  30. .writeable_reg = zpa2326_isreg_writeable,
  31. .readable_reg = zpa2326_isreg_readable,
  32. .precious_reg = zpa2326_isreg_precious,
  33. .max_register = ZPA2326_TEMP_OUT_H_REG,
  34. .read_flag_mask = BIT(7) | BIT(6),
  35. .cache_type = REGCACHE_NONE,
  36. };
  37. static int zpa2326_probe_spi(struct spi_device *spi)
  38. {
  39. struct regmap *regmap;
  40. int err;
  41. regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
  42. if (IS_ERR(regmap)) {
  43. dev_err(&spi->dev, "failed to init registers map");
  44. return PTR_ERR(regmap);
  45. }
  46. /*
  47. * Enforce SPI slave settings to prevent from DT misconfiguration.
  48. *
  49. * Clock is idle high. Sampling happens on trailing edge, i.e., rising
  50. * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
  51. */
  52. spi->mode = SPI_MODE_3;
  53. spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
  54. spi->bits_per_word = 8;
  55. err = spi_setup(spi);
  56. if (err < 0)
  57. return err;
  58. return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
  59. spi->irq, ZPA2326_DEVICE_ID, regmap);
  60. }
  61. static int zpa2326_remove_spi(struct spi_device *spi)
  62. {
  63. zpa2326_remove(&spi->dev);
  64. return 0;
  65. }
  66. static const struct spi_device_id zpa2326_spi_ids[] = {
  67. { "zpa2326", 0 },
  68. { },
  69. };
  70. MODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
  71. #if defined(CONFIG_OF)
  72. static const struct of_device_id zpa2326_spi_matches[] = {
  73. { .compatible = "murata,zpa2326" },
  74. { }
  75. };
  76. MODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
  77. #endif
  78. static struct spi_driver zpa2326_spi_driver = {
  79. .driver = {
  80. .name = "zpa2326-spi",
  81. .of_match_table = of_match_ptr(zpa2326_spi_matches),
  82. .pm = ZPA2326_PM_OPS,
  83. },
  84. .probe = zpa2326_probe_spi,
  85. .remove = zpa2326_remove_spi,
  86. .id_table = zpa2326_spi_ids,
  87. };
  88. module_spi_driver(zpa2326_spi_driver);
  89. MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
  90. MODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
  91. MODULE_LICENSE("GPL v2");