103b262f2SGregor Boirie /*
203b262f2SGregor Boirie  * Murata ZPA2326 I2C pressure and temperature sensor driver
303b262f2SGregor Boirie  *
403b262f2SGregor Boirie  * Copyright (c) 2016 Parrot S.A.
503b262f2SGregor Boirie  *
603b262f2SGregor Boirie  * Author: Gregor Boirie <gregor.boirie@parrot.com>
703b262f2SGregor Boirie  *
803b262f2SGregor Boirie  * This program is free software; you can redistribute it and/or modify it
903b262f2SGregor Boirie  * under the terms of the GNU General Public License version 2 as published by
1003b262f2SGregor Boirie  * the Free Software Foundation.
1103b262f2SGregor Boirie  *
1203b262f2SGregor Boirie  * This program is distributed in the hope that it will be useful, but WITHOUT
1303b262f2SGregor Boirie  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1403b262f2SGregor Boirie  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1503b262f2SGregor Boirie  * more details.
1603b262f2SGregor Boirie  */
1703b262f2SGregor Boirie 
1803b262f2SGregor Boirie #include <linux/module.h>
1903b262f2SGregor Boirie #include <linux/regmap.h>
2003b262f2SGregor Boirie #include <linux/i2c.h>
2103b262f2SGregor Boirie #include <linux/of_device.h>
2203b262f2SGregor Boirie #include "zpa2326.h"
2303b262f2SGregor Boirie 
2403b262f2SGregor Boirie /*
2503b262f2SGregor Boirie  * read_flag_mask:
2603b262f2SGregor Boirie  *   - address bit 7 must be set to request a register read operation
2703b262f2SGregor Boirie  */
2803b262f2SGregor Boirie static const struct regmap_config zpa2326_regmap_i2c_config = {
2903b262f2SGregor Boirie 	.reg_bits       = 8,
3003b262f2SGregor Boirie 	.val_bits       = 8,
3103b262f2SGregor Boirie 	.writeable_reg  = zpa2326_isreg_writeable,
3203b262f2SGregor Boirie 	.readable_reg   = zpa2326_isreg_readable,
3303b262f2SGregor Boirie 	.precious_reg   = zpa2326_isreg_precious,
3403b262f2SGregor Boirie 	.max_register   = ZPA2326_TEMP_OUT_H_REG,
3503b262f2SGregor Boirie 	.read_flag_mask = BIT(7),
3603b262f2SGregor Boirie 	.cache_type     = REGCACHE_NONE,
3703b262f2SGregor Boirie };
3803b262f2SGregor Boirie 
3903b262f2SGregor Boirie static unsigned int zpa2326_i2c_hwid(const struct i2c_client *client)
4003b262f2SGregor Boirie {
4103b262f2SGregor Boirie #define ZPA2326_SA0(_addr)          (_addr & BIT(0))
4203b262f2SGregor Boirie #define ZPA2326_DEVICE_ID_SA0_SHIFT (1)
4303b262f2SGregor Boirie 
4403b262f2SGregor Boirie 	/* Identification register bit 1 mirrors device address bit 0. */
4503b262f2SGregor Boirie 	return (ZPA2326_DEVICE_ID |
4603b262f2SGregor Boirie 		(ZPA2326_SA0(client->addr) << ZPA2326_DEVICE_ID_SA0_SHIFT));
4703b262f2SGregor Boirie }
4803b262f2SGregor Boirie 
4903b262f2SGregor Boirie static int zpa2326_probe_i2c(struct i2c_client          *client,
5003b262f2SGregor Boirie 			     const struct i2c_device_id *i2c_id)
5103b262f2SGregor Boirie {
5203b262f2SGregor Boirie 	struct regmap *regmap;
5303b262f2SGregor Boirie 
5403b262f2SGregor Boirie 	regmap = devm_regmap_init_i2c(client, &zpa2326_regmap_i2c_config);
5503b262f2SGregor Boirie 	if (IS_ERR(regmap)) {
5603b262f2SGregor Boirie 		dev_err(&client->dev, "failed to init registers map");
5703b262f2SGregor Boirie 		return PTR_ERR(regmap);
5803b262f2SGregor Boirie 	}
5903b262f2SGregor Boirie 
6003b262f2SGregor Boirie 	return zpa2326_probe(&client->dev, i2c_id->name, client->irq,
6103b262f2SGregor Boirie 			     zpa2326_i2c_hwid(client), regmap);
6203b262f2SGregor Boirie }
6303b262f2SGregor Boirie 
6403b262f2SGregor Boirie static int zpa2326_remove_i2c(struct i2c_client *client)
6503b262f2SGregor Boirie {
6603b262f2SGregor Boirie 	zpa2326_remove(&client->dev);
6703b262f2SGregor Boirie 
6803b262f2SGregor Boirie 	return 0;
6903b262f2SGregor Boirie }
7003b262f2SGregor Boirie 
7103b262f2SGregor Boirie static const struct i2c_device_id zpa2326_i2c_ids[] = {
7203b262f2SGregor Boirie 	{ "zpa2326", 0 },
7303b262f2SGregor Boirie 	{ },
7403b262f2SGregor Boirie };
7503b262f2SGregor Boirie MODULE_DEVICE_TABLE(i2c, zpa2326_i2c_ids);
7603b262f2SGregor Boirie 
7703b262f2SGregor Boirie #if defined(CONFIG_OF)
7803b262f2SGregor Boirie static const struct of_device_id zpa2326_i2c_matches[] = {
7903b262f2SGregor Boirie 	{ .compatible = "murata,zpa2326" },
8003b262f2SGregor Boirie 	{ }
8103b262f2SGregor Boirie };
8203b262f2SGregor Boirie MODULE_DEVICE_TABLE(of, zpa2326_i2c_matches);
8303b262f2SGregor Boirie #endif
8403b262f2SGregor Boirie 
8503b262f2SGregor Boirie static struct i2c_driver zpa2326_i2c_driver = {
8603b262f2SGregor Boirie 	.driver = {
8703b262f2SGregor Boirie 		.name           = "zpa2326-i2c",
8803b262f2SGregor Boirie 		.of_match_table = of_match_ptr(zpa2326_i2c_matches),
8903b262f2SGregor Boirie 		.pm             = ZPA2326_PM_OPS,
9003b262f2SGregor Boirie 	},
9103b262f2SGregor Boirie 	.probe    = zpa2326_probe_i2c,
9203b262f2SGregor Boirie 	.remove   = zpa2326_remove_i2c,
9303b262f2SGregor Boirie 	.id_table = zpa2326_i2c_ids,
9403b262f2SGregor Boirie };
9503b262f2SGregor Boirie module_i2c_driver(zpa2326_i2c_driver);
9603b262f2SGregor Boirie 
9703b262f2SGregor Boirie MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
9803b262f2SGregor Boirie MODULE_DESCRIPTION("I2C driver for Murata ZPA2326 pressure sensor");
9903b262f2SGregor Boirie MODULE_LICENSE("GPL v2");
100