zpa2326_i2c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Murata ZPA2326 I2C 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/i2c.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. */
  26. static const struct regmap_config zpa2326_regmap_i2c_config = {
  27. .reg_bits = 8,
  28. .val_bits = 8,
  29. .writeable_reg = zpa2326_isreg_writeable,
  30. .readable_reg = zpa2326_isreg_readable,
  31. .precious_reg = zpa2326_isreg_precious,
  32. .max_register = ZPA2326_TEMP_OUT_H_REG,
  33. .read_flag_mask = BIT(7),
  34. .cache_type = REGCACHE_NONE,
  35. };
  36. static unsigned int zpa2326_i2c_hwid(const struct i2c_client *client)
  37. {
  38. #define ZPA2326_SA0(_addr) (_addr & BIT(0))
  39. #define ZPA2326_DEVICE_ID_SA0_SHIFT (1)
  40. /* Identification register bit 1 mirrors device address bit 0. */
  41. return (ZPA2326_DEVICE_ID |
  42. (ZPA2326_SA0(client->addr) << ZPA2326_DEVICE_ID_SA0_SHIFT));
  43. }
  44. static int zpa2326_probe_i2c(struct i2c_client *client,
  45. const struct i2c_device_id *i2c_id)
  46. {
  47. struct regmap *regmap;
  48. regmap = devm_regmap_init_i2c(client, &zpa2326_regmap_i2c_config);
  49. if (IS_ERR(regmap)) {
  50. dev_err(&client->dev, "failed to init registers map");
  51. return PTR_ERR(regmap);
  52. }
  53. return zpa2326_probe(&client->dev, i2c_id->name, client->irq,
  54. zpa2326_i2c_hwid(client), regmap);
  55. }
  56. static int zpa2326_remove_i2c(struct i2c_client *client)
  57. {
  58. zpa2326_remove(&client->dev);
  59. return 0;
  60. }
  61. static const struct i2c_device_id zpa2326_i2c_ids[] = {
  62. { "zpa2326", 0 },
  63. { },
  64. };
  65. MODULE_DEVICE_TABLE(i2c, zpa2326_i2c_ids);
  66. #if defined(CONFIG_OF)
  67. static const struct of_device_id zpa2326_i2c_matches[] = {
  68. { .compatible = "murata,zpa2326" },
  69. { }
  70. };
  71. MODULE_DEVICE_TABLE(of, zpa2326_i2c_matches);
  72. #endif
  73. static struct i2c_driver zpa2326_i2c_driver = {
  74. .driver = {
  75. .name = "zpa2326-i2c",
  76. .of_match_table = of_match_ptr(zpa2326_i2c_matches),
  77. .pm = ZPA2326_PM_OPS,
  78. },
  79. .probe = zpa2326_probe_i2c,
  80. .remove = zpa2326_remove_i2c,
  81. .id_table = zpa2326_i2c_ids,
  82. };
  83. module_i2c_driver(zpa2326_i2c_driver);
  84. MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
  85. MODULE_DESCRIPTION("I2C driver for Murata ZPA2326 pressure sensor");
  86. MODULE_LICENSE("GPL v2");