xref: /linux/drivers/iio/pressure/zpa2326_i2c.c (revision 03b262f2bbf43b82eaef82ffb3bc671d5b5c8da1)
1*03b262f2SGregor Boirie /*
2*03b262f2SGregor Boirie  * Murata ZPA2326 I2C pressure and temperature sensor driver
3*03b262f2SGregor Boirie  *
4*03b262f2SGregor Boirie  * Copyright (c) 2016 Parrot S.A.
5*03b262f2SGregor Boirie  *
6*03b262f2SGregor Boirie  * Author: Gregor Boirie <gregor.boirie@parrot.com>
7*03b262f2SGregor Boirie  *
8*03b262f2SGregor Boirie  * This program is free software; you can redistribute it and/or modify it
9*03b262f2SGregor Boirie  * under the terms of the GNU General Public License version 2 as published by
10*03b262f2SGregor Boirie  * the Free Software Foundation.
11*03b262f2SGregor Boirie  *
12*03b262f2SGregor Boirie  * This program is distributed in the hope that it will be useful, but WITHOUT
13*03b262f2SGregor Boirie  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*03b262f2SGregor Boirie  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15*03b262f2SGregor Boirie  * more details.
16*03b262f2SGregor Boirie  */
17*03b262f2SGregor Boirie 
18*03b262f2SGregor Boirie #include <linux/module.h>
19*03b262f2SGregor Boirie #include <linux/regmap.h>
20*03b262f2SGregor Boirie #include <linux/i2c.h>
21*03b262f2SGregor Boirie #include <linux/of_device.h>
22*03b262f2SGregor Boirie #include "zpa2326.h"
23*03b262f2SGregor Boirie 
24*03b262f2SGregor Boirie /*
25*03b262f2SGregor Boirie  * read_flag_mask:
26*03b262f2SGregor Boirie  *   - address bit 7 must be set to request a register read operation
27*03b262f2SGregor Boirie  */
28*03b262f2SGregor Boirie static const struct regmap_config zpa2326_regmap_i2c_config = {
29*03b262f2SGregor Boirie 	.reg_bits       = 8,
30*03b262f2SGregor Boirie 	.val_bits       = 8,
31*03b262f2SGregor Boirie 	.writeable_reg  = zpa2326_isreg_writeable,
32*03b262f2SGregor Boirie 	.readable_reg   = zpa2326_isreg_readable,
33*03b262f2SGregor Boirie 	.precious_reg   = zpa2326_isreg_precious,
34*03b262f2SGregor Boirie 	.max_register   = ZPA2326_TEMP_OUT_H_REG,
35*03b262f2SGregor Boirie 	.read_flag_mask = BIT(7),
36*03b262f2SGregor Boirie 	.cache_type     = REGCACHE_NONE,
37*03b262f2SGregor Boirie };
38*03b262f2SGregor Boirie 
39*03b262f2SGregor Boirie static unsigned int zpa2326_i2c_hwid(const struct i2c_client *client)
40*03b262f2SGregor Boirie {
41*03b262f2SGregor Boirie #define ZPA2326_SA0(_addr)          (_addr & BIT(0))
42*03b262f2SGregor Boirie #define ZPA2326_DEVICE_ID_SA0_SHIFT (1)
43*03b262f2SGregor Boirie 
44*03b262f2SGregor Boirie 	/* Identification register bit 1 mirrors device address bit 0. */
45*03b262f2SGregor Boirie 	return (ZPA2326_DEVICE_ID |
46*03b262f2SGregor Boirie 		(ZPA2326_SA0(client->addr) << ZPA2326_DEVICE_ID_SA0_SHIFT));
47*03b262f2SGregor Boirie }
48*03b262f2SGregor Boirie 
49*03b262f2SGregor Boirie static int zpa2326_probe_i2c(struct i2c_client          *client,
50*03b262f2SGregor Boirie 			     const struct i2c_device_id *i2c_id)
51*03b262f2SGregor Boirie {
52*03b262f2SGregor Boirie 	struct regmap *regmap;
53*03b262f2SGregor Boirie 
54*03b262f2SGregor Boirie 	regmap = devm_regmap_init_i2c(client, &zpa2326_regmap_i2c_config);
55*03b262f2SGregor Boirie 	if (IS_ERR(regmap)) {
56*03b262f2SGregor Boirie 		dev_err(&client->dev, "failed to init registers map");
57*03b262f2SGregor Boirie 		return PTR_ERR(regmap);
58*03b262f2SGregor Boirie 	}
59*03b262f2SGregor Boirie 
60*03b262f2SGregor Boirie 	return zpa2326_probe(&client->dev, i2c_id->name, client->irq,
61*03b262f2SGregor Boirie 			     zpa2326_i2c_hwid(client), regmap);
62*03b262f2SGregor Boirie }
63*03b262f2SGregor Boirie 
64*03b262f2SGregor Boirie static int zpa2326_remove_i2c(struct i2c_client *client)
65*03b262f2SGregor Boirie {
66*03b262f2SGregor Boirie 	zpa2326_remove(&client->dev);
67*03b262f2SGregor Boirie 
68*03b262f2SGregor Boirie 	return 0;
69*03b262f2SGregor Boirie }
70*03b262f2SGregor Boirie 
71*03b262f2SGregor Boirie static const struct i2c_device_id zpa2326_i2c_ids[] = {
72*03b262f2SGregor Boirie 	{ "zpa2326", 0 },
73*03b262f2SGregor Boirie 	{ },
74*03b262f2SGregor Boirie };
75*03b262f2SGregor Boirie MODULE_DEVICE_TABLE(i2c, zpa2326_i2c_ids);
76*03b262f2SGregor Boirie 
77*03b262f2SGregor Boirie #if defined(CONFIG_OF)
78*03b262f2SGregor Boirie static const struct of_device_id zpa2326_i2c_matches[] = {
79*03b262f2SGregor Boirie 	{ .compatible = "murata,zpa2326" },
80*03b262f2SGregor Boirie 	{ }
81*03b262f2SGregor Boirie };
82*03b262f2SGregor Boirie MODULE_DEVICE_TABLE(of, zpa2326_i2c_matches);
83*03b262f2SGregor Boirie #endif
84*03b262f2SGregor Boirie 
85*03b262f2SGregor Boirie static struct i2c_driver zpa2326_i2c_driver = {
86*03b262f2SGregor Boirie 	.driver = {
87*03b262f2SGregor Boirie 		.name           = "zpa2326-i2c",
88*03b262f2SGregor Boirie 		.of_match_table = of_match_ptr(zpa2326_i2c_matches),
89*03b262f2SGregor Boirie 		.pm             = ZPA2326_PM_OPS,
90*03b262f2SGregor Boirie 	},
91*03b262f2SGregor Boirie 	.probe    = zpa2326_probe_i2c,
92*03b262f2SGregor Boirie 	.remove   = zpa2326_remove_i2c,
93*03b262f2SGregor Boirie 	.id_table = zpa2326_i2c_ids,
94*03b262f2SGregor Boirie };
95*03b262f2SGregor Boirie module_i2c_driver(zpa2326_i2c_driver);
96*03b262f2SGregor Boirie 
97*03b262f2SGregor Boirie MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
98*03b262f2SGregor Boirie MODULE_DESCRIPTION("I2C driver for Murata ZPA2326 pressure sensor");
99*03b262f2SGregor Boirie MODULE_LICENSE("GPL v2");
100