18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * JSA1212 Ambient Light & Proximity Sensor Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2014, Intel Corporation.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * JSA1212 I2C slave address: 0x44(ADDR tied to GND), 0x45(ADDR tied to VDD)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * TODO: Interrupt support, thresholds, range support.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/i2c.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/acpi.h>
198c2ecf20Sopenharmony_ci#include <linux/regmap.h>
208c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
218c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* JSA1212 reg address */
248c2ecf20Sopenharmony_ci#define JSA1212_CONF_REG		0x01
258c2ecf20Sopenharmony_ci#define JSA1212_INT_REG			0x02
268c2ecf20Sopenharmony_ci#define JSA1212_PXS_LT_REG		0x03
278c2ecf20Sopenharmony_ci#define JSA1212_PXS_HT_REG		0x04
288c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH1_REG		0x05
298c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH2_REG		0x06
308c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH3_REG		0x07
318c2ecf20Sopenharmony_ci#define JSA1212_PXS_DATA_REG		0x08
328c2ecf20Sopenharmony_ci#define JSA1212_ALS_DT1_REG		0x09
338c2ecf20Sopenharmony_ci#define JSA1212_ALS_DT2_REG		0x0A
348c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_REG		0x0B
358c2ecf20Sopenharmony_ci#define JSA1212_MAX_REG			0x0C
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* JSA1212 reg masks */
388c2ecf20Sopenharmony_ci#define JSA1212_CONF_MASK		0xFF
398c2ecf20Sopenharmony_ci#define JSA1212_INT_MASK		0xFF
408c2ecf20Sopenharmony_ci#define JSA1212_PXS_LT_MASK		0xFF
418c2ecf20Sopenharmony_ci#define JSA1212_PXS_HT_MASK		0xFF
428c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH1_MASK		0xFF
438c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH2_LT_MASK		0x0F
448c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH2_HT_MASK		0xF0
458c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH3_MASK		0xFF
468c2ecf20Sopenharmony_ci#define JSA1212_PXS_DATA_MASK		0xFF
478c2ecf20Sopenharmony_ci#define JSA1212_ALS_DATA_MASK		0x0FFF
488c2ecf20Sopenharmony_ci#define JSA1212_ALS_DT1_MASK		0xFF
498c2ecf20Sopenharmony_ci#define JSA1212_ALS_DT2_MASK		0x0F
508c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_MASK		0x07
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* JSA1212 CONF REG bits */
538c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_MASK		0x80
548c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_ENABLE		0x80
558c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_DISABLE	0x00
568c2ecf20Sopenharmony_ci#define JSA1212_CONF_ALS_MASK		0x04
578c2ecf20Sopenharmony_ci#define JSA1212_CONF_ALS_ENABLE		0x04
588c2ecf20Sopenharmony_ci#define JSA1212_CONF_ALS_DISABLE	0x00
598c2ecf20Sopenharmony_ci#define JSA1212_CONF_IRDR_MASK		0x08
608c2ecf20Sopenharmony_ci/* Proxmity sensing IRDR current sink settings */
618c2ecf20Sopenharmony_ci#define JSA1212_CONF_IRDR_200MA		0x08
628c2ecf20Sopenharmony_ci#define JSA1212_CONF_IRDR_100MA		0x00
638c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_MASK	0x70
648c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_0MS	0x70
658c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_12MS	0x60
668c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_50MS	0x50
678c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_75MS	0x40
688c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_100MS	0x30
698c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_200MS	0x20
708c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_400MS	0x10
718c2ecf20Sopenharmony_ci#define JSA1212_CONF_PXS_SLP_800MS	0x00
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* JSA1212 INT REG bits */
748c2ecf20Sopenharmony_ci#define JSA1212_INT_CTRL_MASK		0x01
758c2ecf20Sopenharmony_ci#define JSA1212_INT_CTRL_EITHER		0x00
768c2ecf20Sopenharmony_ci#define JSA1212_INT_CTRL_BOTH		0x01
778c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_PRST_MASK	0x06
788c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_PRST_1CONV	0x00
798c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_PRST_4CONV	0x02
808c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_PRST_8CONV	0x04
818c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_PRST_16CONV	0x06
828c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_FLAG_MASK	0x08
838c2ecf20Sopenharmony_ci#define JSA1212_INT_ALS_FLAG_CLR	0x00
848c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_PRST_MASK	0x60
858c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_PRST_1CONV	0x00
868c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_PRST_4CONV	0x20
878c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_PRST_8CONV	0x40
888c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_PRST_16CONV	0x60
898c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_FLAG_MASK	0x80
908c2ecf20Sopenharmony_ci#define JSA1212_INT_PXS_FLAG_CLR	0x00
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* JSA1212 ALS RNG REG bits */
938c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_0_2048		0x00
948c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_0_1024		0x01
958c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_0_512		0x02
968c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_0_256		0x03
978c2ecf20Sopenharmony_ci#define JSA1212_ALS_RNG_0_128		0x04
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* JSA1212 INT threshold range */
1008c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH_MIN	0x0000
1018c2ecf20Sopenharmony_ci#define JSA1212_ALS_TH_MAX	0x0FFF
1028c2ecf20Sopenharmony_ci#define JSA1212_PXS_TH_MIN	0x00
1038c2ecf20Sopenharmony_ci#define JSA1212_PXS_TH_MAX	0xFF
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define JSA1212_ALS_DELAY_MS	200
1068c2ecf20Sopenharmony_ci#define JSA1212_PXS_DELAY_MS	100
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define JSA1212_DRIVER_NAME	"jsa1212"
1098c2ecf20Sopenharmony_ci#define JSA1212_REGMAP_NAME	"jsa1212_regmap"
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cienum jsa1212_op_mode {
1128c2ecf20Sopenharmony_ci	JSA1212_OPMODE_ALS_EN,
1138c2ecf20Sopenharmony_ci	JSA1212_OPMODE_PXS_EN,
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistruct jsa1212_data {
1178c2ecf20Sopenharmony_ci	struct i2c_client *client;
1188c2ecf20Sopenharmony_ci	struct mutex lock;
1198c2ecf20Sopenharmony_ci	u8 als_rng_idx;
1208c2ecf20Sopenharmony_ci	bool als_en; /* ALS enable status */
1218c2ecf20Sopenharmony_ci	bool pxs_en; /* proximity enable status */
1228c2ecf20Sopenharmony_ci	struct regmap *regmap;
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* ALS range idx to val mapping */
1268c2ecf20Sopenharmony_cistatic const int jsa1212_als_range_val[] = {2048, 1024, 512, 256, 128,
1278c2ecf20Sopenharmony_ci						128, 128, 128};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* Enables or disables ALS function based on status */
1308c2ecf20Sopenharmony_cistatic int jsa1212_als_enable(struct jsa1212_data *data, u8 status)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int ret;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
1358c2ecf20Sopenharmony_ci				JSA1212_CONF_ALS_MASK,
1368c2ecf20Sopenharmony_ci				status);
1378c2ecf20Sopenharmony_ci	if (ret < 0)
1388c2ecf20Sopenharmony_ci		return ret;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	data->als_en = !!status;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return 0;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* Enables or disables PXS function based on status */
1468c2ecf20Sopenharmony_cistatic int jsa1212_pxs_enable(struct jsa1212_data *data, u8 status)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int ret;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
1518c2ecf20Sopenharmony_ci				JSA1212_CONF_PXS_MASK,
1528c2ecf20Sopenharmony_ci				status);
1538c2ecf20Sopenharmony_ci	if (ret < 0)
1548c2ecf20Sopenharmony_ci		return ret;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	data->pxs_en = !!status;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return 0;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int jsa1212_read_als_data(struct jsa1212_data *data,
1628c2ecf20Sopenharmony_ci				unsigned int *val)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	int ret;
1658c2ecf20Sopenharmony_ci	__le16 als_data;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
1688c2ecf20Sopenharmony_ci	if (ret < 0)
1698c2ecf20Sopenharmony_ci		return ret;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Delay for data output */
1728c2ecf20Sopenharmony_ci	msleep(JSA1212_ALS_DELAY_MS);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/* Read 12 bit data */
1758c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(data->regmap, JSA1212_ALS_DT1_REG, &als_data, 2);
1768c2ecf20Sopenharmony_ci	if (ret < 0) {
1778c2ecf20Sopenharmony_ci		dev_err(&data->client->dev, "als data read err\n");
1788c2ecf20Sopenharmony_ci		goto als_data_read_err;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	*val = le16_to_cpu(als_data);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cials_data_read_err:
1848c2ecf20Sopenharmony_ci	return jsa1212_als_enable(data, JSA1212_CONF_ALS_DISABLE);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int jsa1212_read_pxs_data(struct jsa1212_data *data,
1888c2ecf20Sopenharmony_ci				unsigned int *val)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	int ret;
1918c2ecf20Sopenharmony_ci	unsigned int pxs_data;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
1948c2ecf20Sopenharmony_ci	if (ret < 0)
1958c2ecf20Sopenharmony_ci		return ret;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Delay for data output */
1988c2ecf20Sopenharmony_ci	msleep(JSA1212_PXS_DELAY_MS);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Read out all data */
2018c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, JSA1212_PXS_DATA_REG, &pxs_data);
2028c2ecf20Sopenharmony_ci	if (ret < 0) {
2038c2ecf20Sopenharmony_ci		dev_err(&data->client->dev, "pxs data read err\n");
2048c2ecf20Sopenharmony_ci		goto pxs_data_read_err;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	*val = pxs_data & JSA1212_PXS_DATA_MASK;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cipxs_data_read_err:
2108c2ecf20Sopenharmony_ci	return jsa1212_pxs_enable(data, JSA1212_CONF_PXS_DISABLE);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int jsa1212_read_raw(struct iio_dev *indio_dev,
2148c2ecf20Sopenharmony_ci				struct iio_chan_spec const *chan,
2158c2ecf20Sopenharmony_ci				int *val, int *val2, long mask)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	int ret;
2188c2ecf20Sopenharmony_ci	struct jsa1212_data *data = iio_priv(indio_dev);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	switch (mask) {
2218c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
2228c2ecf20Sopenharmony_ci		mutex_lock(&data->lock);
2238c2ecf20Sopenharmony_ci		switch (chan->type) {
2248c2ecf20Sopenharmony_ci		case IIO_LIGHT:
2258c2ecf20Sopenharmony_ci			ret = jsa1212_read_als_data(data, val);
2268c2ecf20Sopenharmony_ci			break;
2278c2ecf20Sopenharmony_ci		case IIO_PROXIMITY:
2288c2ecf20Sopenharmony_ci			ret = jsa1212_read_pxs_data(data, val);
2298c2ecf20Sopenharmony_ci			break;
2308c2ecf20Sopenharmony_ci		default:
2318c2ecf20Sopenharmony_ci			ret = -EINVAL;
2328c2ecf20Sopenharmony_ci			break;
2338c2ecf20Sopenharmony_ci		}
2348c2ecf20Sopenharmony_ci		mutex_unlock(&data->lock);
2358c2ecf20Sopenharmony_ci		return ret < 0 ? ret : IIO_VAL_INT;
2368c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
2378c2ecf20Sopenharmony_ci		switch (chan->type) {
2388c2ecf20Sopenharmony_ci		case IIO_LIGHT:
2398c2ecf20Sopenharmony_ci			*val = jsa1212_als_range_val[data->als_rng_idx];
2408c2ecf20Sopenharmony_ci			*val2 = BIT(12); /* Max 12 bit value */
2418c2ecf20Sopenharmony_ci			return IIO_VAL_FRACTIONAL;
2428c2ecf20Sopenharmony_ci		default:
2438c2ecf20Sopenharmony_ci			break;
2448c2ecf20Sopenharmony_ci		}
2458c2ecf20Sopenharmony_ci		break;
2468c2ecf20Sopenharmony_ci	default:
2478c2ecf20Sopenharmony_ci		break;
2488c2ecf20Sopenharmony_ci	}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	return -EINVAL;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic const struct iio_chan_spec jsa1212_channels[] = {
2548c2ecf20Sopenharmony_ci	{
2558c2ecf20Sopenharmony_ci		.type = IIO_LIGHT,
2568c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
2578c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE),
2588c2ecf20Sopenharmony_ci	},
2598c2ecf20Sopenharmony_ci	{
2608c2ecf20Sopenharmony_ci		.type = IIO_PROXIMITY,
2618c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci};
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic const struct iio_info jsa1212_info = {
2668c2ecf20Sopenharmony_ci	.read_raw		= &jsa1212_read_raw,
2678c2ecf20Sopenharmony_ci};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int jsa1212_chip_init(struct jsa1212_data *data)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int ret;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ret = regmap_write(data->regmap, JSA1212_CONF_REG,
2748c2ecf20Sopenharmony_ci				(JSA1212_CONF_PXS_SLP_50MS |
2758c2ecf20Sopenharmony_ci				JSA1212_CONF_IRDR_200MA));
2768c2ecf20Sopenharmony_ci	if (ret < 0)
2778c2ecf20Sopenharmony_ci		return ret;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	ret = regmap_write(data->regmap, JSA1212_INT_REG,
2808c2ecf20Sopenharmony_ci				JSA1212_INT_ALS_PRST_4CONV);
2818c2ecf20Sopenharmony_ci	if (ret < 0)
2828c2ecf20Sopenharmony_ci		return ret;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	data->als_rng_idx = JSA1212_ALS_RNG_0_2048;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return 0;
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	switch (reg) {
2928c2ecf20Sopenharmony_ci	case JSA1212_PXS_DATA_REG:
2938c2ecf20Sopenharmony_ci	case JSA1212_ALS_DT1_REG:
2948c2ecf20Sopenharmony_ci	case JSA1212_ALS_DT2_REG:
2958c2ecf20Sopenharmony_ci	case JSA1212_INT_REG:
2968c2ecf20Sopenharmony_ci		return true;
2978c2ecf20Sopenharmony_ci	default:
2988c2ecf20Sopenharmony_ci		return false;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic const struct regmap_config jsa1212_regmap_config = {
3038c2ecf20Sopenharmony_ci	.name =  JSA1212_REGMAP_NAME,
3048c2ecf20Sopenharmony_ci	.reg_bits = 8,
3058c2ecf20Sopenharmony_ci	.val_bits = 8,
3068c2ecf20Sopenharmony_ci	.max_register = JSA1212_MAX_REG,
3078c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
3088c2ecf20Sopenharmony_ci	.volatile_reg = jsa1212_is_volatile_reg,
3098c2ecf20Sopenharmony_ci};
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic int jsa1212_probe(struct i2c_client *client,
3128c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	struct jsa1212_data *data;
3158c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
3168c2ecf20Sopenharmony_ci	struct regmap *regmap;
3178c2ecf20Sopenharmony_ci	int ret;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
3208c2ecf20Sopenharmony_ci	if (!indio_dev)
3218c2ecf20Sopenharmony_ci		return -ENOMEM;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
3248c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
3258c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Regmap initialization failed.\n");
3268c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	data = iio_priv(indio_dev);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, indio_dev);
3328c2ecf20Sopenharmony_ci	data->client = client;
3338c2ecf20Sopenharmony_ci	data->regmap = regmap;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	mutex_init(&data->lock);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	ret = jsa1212_chip_init(data);
3388c2ecf20Sopenharmony_ci	if (ret < 0)
3398c2ecf20Sopenharmony_ci		return ret;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	indio_dev->channels = jsa1212_channels;
3428c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
3438c2ecf20Sopenharmony_ci	indio_dev->name = JSA1212_DRIVER_NAME;
3448c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	indio_dev->info = &jsa1212_info;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	ret = iio_device_register(indio_dev);
3498c2ecf20Sopenharmony_ci	if (ret < 0)
3508c2ecf20Sopenharmony_ci		dev_err(&client->dev, "%s: register device failed\n", __func__);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	return ret;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci /* power off the device */
3568c2ecf20Sopenharmony_cistatic int jsa1212_power_off(struct jsa1212_data *data)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	int ret;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
3638c2ecf20Sopenharmony_ci				JSA1212_CONF_ALS_MASK |
3648c2ecf20Sopenharmony_ci				JSA1212_CONF_PXS_MASK,
3658c2ecf20Sopenharmony_ci				JSA1212_CONF_ALS_DISABLE |
3668c2ecf20Sopenharmony_ci				JSA1212_CONF_PXS_DISABLE);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	if (ret < 0)
3698c2ecf20Sopenharmony_ci		dev_err(&data->client->dev, "power off cmd failed\n");
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	return ret;
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic int jsa1212_remove(struct i2c_client *client)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = i2c_get_clientdata(client);
3798c2ecf20Sopenharmony_ci	struct jsa1212_data *data = iio_priv(indio_dev);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	iio_device_unregister(indio_dev);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return jsa1212_power_off(data);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
3878c2ecf20Sopenharmony_cistatic int jsa1212_suspend(struct device *dev)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct jsa1212_data *data;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	return jsa1212_power_off(data);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic int jsa1212_resume(struct device *dev)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	int ret = 0;
3998c2ecf20Sopenharmony_ci	struct jsa1212_data *data;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if (data->als_en) {
4068c2ecf20Sopenharmony_ci		ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
4078c2ecf20Sopenharmony_ci		if (ret < 0) {
4088c2ecf20Sopenharmony_ci			dev_err(dev, "als resume failed\n");
4098c2ecf20Sopenharmony_ci			goto unlock_and_ret;
4108c2ecf20Sopenharmony_ci		}
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (data->pxs_en) {
4148c2ecf20Sopenharmony_ci		ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
4158c2ecf20Sopenharmony_ci		if (ret < 0)
4168c2ecf20Sopenharmony_ci			dev_err(dev, "pxs resume failed\n");
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ciunlock_and_ret:
4208c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
4218c2ecf20Sopenharmony_ci	return ret;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci#define JSA1212_PM_OPS (&jsa1212_pm_ops)
4278c2ecf20Sopenharmony_ci#else
4288c2ecf20Sopenharmony_ci#define JSA1212_PM_OPS NULL
4298c2ecf20Sopenharmony_ci#endif
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic const struct acpi_device_id jsa1212_acpi_match[] = {
4328c2ecf20Sopenharmony_ci	{"JSA1212", 0},
4338c2ecf20Sopenharmony_ci	{ },
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic const struct i2c_device_id jsa1212_id[] = {
4388c2ecf20Sopenharmony_ci	{ JSA1212_DRIVER_NAME, 0 },
4398c2ecf20Sopenharmony_ci	{ }
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, jsa1212_id);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic struct i2c_driver jsa1212_driver = {
4448c2ecf20Sopenharmony_ci	.driver = {
4458c2ecf20Sopenharmony_ci		.name	= JSA1212_DRIVER_NAME,
4468c2ecf20Sopenharmony_ci		.pm	= JSA1212_PM_OPS,
4478c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
4488c2ecf20Sopenharmony_ci	},
4498c2ecf20Sopenharmony_ci	.probe		= jsa1212_probe,
4508c2ecf20Sopenharmony_ci	.remove		= jsa1212_remove,
4518c2ecf20Sopenharmony_ci	.id_table	= jsa1212_id,
4528c2ecf20Sopenharmony_ci};
4538c2ecf20Sopenharmony_cimodule_i2c_driver(jsa1212_driver);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
4568c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
4578c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
458