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