18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Murata ZPA2326 I2C pressure and temperature sensor driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2016 Parrot S.A.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Gregor Boirie <gregor.boirie@parrot.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/regmap.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
148c2ecf20Sopenharmony_ci#include "zpa2326.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * read_flag_mask:
188c2ecf20Sopenharmony_ci *   - address bit 7 must be set to request a register read operation
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_cistatic const struct regmap_config zpa2326_regmap_i2c_config = {
218c2ecf20Sopenharmony_ci	.reg_bits       = 8,
228c2ecf20Sopenharmony_ci	.val_bits       = 8,
238c2ecf20Sopenharmony_ci	.writeable_reg  = zpa2326_isreg_writeable,
248c2ecf20Sopenharmony_ci	.readable_reg   = zpa2326_isreg_readable,
258c2ecf20Sopenharmony_ci	.precious_reg   = zpa2326_isreg_precious,
268c2ecf20Sopenharmony_ci	.max_register   = ZPA2326_TEMP_OUT_H_REG,
278c2ecf20Sopenharmony_ci	.read_flag_mask = BIT(7),
288c2ecf20Sopenharmony_ci	.cache_type     = REGCACHE_NONE,
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic unsigned int zpa2326_i2c_hwid(const struct i2c_client *client)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci#define ZPA2326_SA0(_addr)          (_addr & BIT(0))
348c2ecf20Sopenharmony_ci#define ZPA2326_DEVICE_ID_SA0_SHIFT (1)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/* Identification register bit 1 mirrors device address bit 0. */
378c2ecf20Sopenharmony_ci	return (ZPA2326_DEVICE_ID |
388c2ecf20Sopenharmony_ci		(ZPA2326_SA0(client->addr) << ZPA2326_DEVICE_ID_SA0_SHIFT));
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic int zpa2326_probe_i2c(struct i2c_client          *client,
428c2ecf20Sopenharmony_ci			     const struct i2c_device_id *i2c_id)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct regmap *regmap;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(client, &zpa2326_regmap_i2c_config);
478c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
488c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to init registers map");
498c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	return zpa2326_probe(&client->dev, i2c_id->name, client->irq,
538c2ecf20Sopenharmony_ci			     zpa2326_i2c_hwid(client), regmap);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int zpa2326_remove_i2c(struct i2c_client *client)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	zpa2326_remove(&client->dev);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return 0;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct i2c_device_id zpa2326_i2c_ids[] = {
648c2ecf20Sopenharmony_ci	{ "zpa2326", 0 },
658c2ecf20Sopenharmony_ci	{ },
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, zpa2326_i2c_ids);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const struct of_device_id zpa2326_i2c_matches[] = {
708c2ecf20Sopenharmony_ci	{ .compatible = "murata,zpa2326" },
718c2ecf20Sopenharmony_ci	{ }
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, zpa2326_i2c_matches);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic struct i2c_driver zpa2326_i2c_driver = {
768c2ecf20Sopenharmony_ci	.driver = {
778c2ecf20Sopenharmony_ci		.name           = "zpa2326-i2c",
788c2ecf20Sopenharmony_ci		.of_match_table = zpa2326_i2c_matches,
798c2ecf20Sopenharmony_ci		.pm             = ZPA2326_PM_OPS,
808c2ecf20Sopenharmony_ci	},
818c2ecf20Sopenharmony_ci	.probe    = zpa2326_probe_i2c,
828c2ecf20Sopenharmony_ci	.remove   = zpa2326_remove_i2c,
838c2ecf20Sopenharmony_ci	.id_table = zpa2326_i2c_ids,
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_cimodule_i2c_driver(zpa2326_i2c_driver);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciMODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
888c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("I2C driver for Murata ZPA2326 pressure sensor");
898c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
90