zpa2326_i2c.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Murata ZPA2326 I2C pressure and temperature sensor driver
  4. *
  5. * Copyright (c) 2016 Parrot S.A.
  6. *
  7. * Author: Gregor Boirie <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mod_devicetable.h>
  13. #include "zpa2326.h"
  14. /*
  15. * read_flag_mask:
  16. * - address bit 7 must be set to request a register read operation
  17. */
  18. static const struct regmap_config zpa2326_regmap_i2c_config = {
  19. .reg_bits = 8,
  20. .val_bits = 8,
  21. .writeable_reg = zpa2326_isreg_writeable,
  22. .readable_reg = zpa2326_isreg_readable,
  23. .precious_reg = zpa2326_isreg_precious,
  24. .max_register = ZPA2326_TEMP_OUT_H_REG,
  25. .read_flag_mask = BIT(7),
  26. .cache_type = REGCACHE_NONE,
  27. };
  28. static unsigned int zpa2326_i2c_hwid(const struct i2c_client *client)
  29. {
  30. #define ZPA2326_SA0(_addr) (_addr & BIT(0))
  31. #define ZPA2326_DEVICE_ID_SA0_SHIFT (1)
  32. /* Identification register bit 1 mirrors device address bit 0. */
  33. return (ZPA2326_DEVICE_ID |
  34. (ZPA2326_SA0(client->addr) << ZPA2326_DEVICE_ID_SA0_SHIFT));
  35. }
  36. static int zpa2326_probe_i2c(struct i2c_client *client,
  37. const struct i2c_device_id *i2c_id)
  38. {
  39. struct regmap *regmap;
  40. regmap = devm_regmap_init_i2c(client, &zpa2326_regmap_i2c_config);
  41. if (IS_ERR(regmap)) {
  42. dev_err(&client->dev, "failed to init registers map");
  43. return PTR_ERR(regmap);
  44. }
  45. return zpa2326_probe(&client->dev, i2c_id->name, client->irq,
  46. zpa2326_i2c_hwid(client), regmap);
  47. }
  48. static void zpa2326_remove_i2c(struct i2c_client *client)
  49. {
  50. zpa2326_remove(&client->dev);
  51. }
  52. static const struct i2c_device_id zpa2326_i2c_ids[] = {
  53. { "zpa2326", 0 },
  54. { },
  55. };
  56. MODULE_DEVICE_TABLE(i2c, zpa2326_i2c_ids);
  57. static const struct of_device_id zpa2326_i2c_matches[] = {
  58. { .compatible = "murata,zpa2326" },
  59. { }
  60. };
  61. MODULE_DEVICE_TABLE(of, zpa2326_i2c_matches);
  62. static struct i2c_driver zpa2326_i2c_driver = {
  63. .driver = {
  64. .name = "zpa2326-i2c",
  65. .of_match_table = zpa2326_i2c_matches,
  66. .pm = ZPA2326_PM_OPS,
  67. },
  68. .probe = zpa2326_probe_i2c,
  69. .remove = zpa2326_remove_i2c,
  70. .id_table = zpa2326_i2c_ids,
  71. };
  72. module_i2c_driver(zpa2326_i2c_driver);
  73. MODULE_AUTHOR("Gregor Boirie <[email protected]>");
  74. MODULE_DESCRIPTION("I2C driver for Murata ZPA2326 pressure sensor");
  75. MODULE_LICENSE("GPL v2");
  76. MODULE_IMPORT_NS(IIO_ZPA2326);