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