Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2016 Marek Vasut <marex@denx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Driver for Hope RF HP03 digital temperature and pressure sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) "hp03: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/iio/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * The HP03 sensor occupies two fixed I2C addresses:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *  0x50 ... read-only EEPROM with calibration data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *  0x77 ... read-write ADC for pressure and temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define HP03_EEPROM_ADDR		0x50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define HP03_ADC_ADDR			0x77
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define HP03_EEPROM_CX_OFFSET		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define HP03_EEPROM_AB_OFFSET		0x1e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define HP03_EEPROM_CD_OFFSET		0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define HP03_ADC_WRITE_REG		0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define HP03_ADC_READ_REG		0xfd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define HP03_ADC_READ_PRESSURE		0xf0	/* D1 in datasheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define HP03_ADC_READ_TEMP		0xe8	/* D2 in datasheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) struct hp03_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct mutex		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct gpio_desc	*xclr_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct i2c_client	*eeprom_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct regmap		*eeprom_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	s32			pressure;	/* kPa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	s32			temp;		/* Deg. C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static const struct iio_chan_spec hp03_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		.type = IIO_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.type = IIO_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static bool hp03_is_writeable_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static bool hp03_is_volatile_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static const struct regmap_config hp03_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	.reg_bits	= 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	.val_bits	= 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	.max_register	= HP03_EEPROM_CD_OFFSET + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.cache_type	= REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	.writeable_reg	= hp03_is_writeable_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	.volatile_reg	= hp03_is_volatile_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int hp03_get_temp_pressure(struct hp03_priv *priv, const u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	ret = i2c_smbus_write_byte_data(priv->client, HP03_ADC_WRITE_REG, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	msleep(50);	/* Wait for conversion to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return i2c_smbus_read_word_data(priv->client, HP03_ADC_READ_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int hp03_update_temp_pressure(struct hp03_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct device *dev = &priv->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u8 coefs[18];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u16 cx_val[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int ab_val, d1_val, d2_val, diff_val, dut, off, sens, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/* Sample coefficients from EEPROM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ret = regmap_bulk_read(priv->eeprom_regmap, HP03_EEPROM_CX_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			       coefs, sizeof(coefs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		dev_err(dev, "Failed to read EEPROM (reg=%02x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			HP03_EEPROM_CX_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/* Sample Temperature and Pressure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	gpiod_set_value_cansleep(priv->xclr_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_PRESSURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		dev_err(dev, "Failed to read pressure\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		goto err_adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	d1_val = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		dev_err(dev, "Failed to read temperature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		goto err_adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	d2_val = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	gpiod_set_value_cansleep(priv->xclr_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* The Cx coefficients and Temp/Pressure values are MSB first. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	for (i = 0; i < 7; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		cx_val[i] = (coefs[2 * i] << 8) | (coefs[(2 * i) + 1] << 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	d1_val = ((d1_val >> 8) & 0xff) | ((d1_val & 0xff) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	d2_val = ((d2_val >> 8) & 0xff) | ((d2_val & 0xff) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	/* Coefficient voodoo from the HP03 datasheet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (d2_val >= cx_val[4])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		ab_val = coefs[14];	/* A-value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ab_val = coefs[15];	/* B-value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	diff_val = d2_val - cx_val[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	dut = (ab_val * (diff_val >> 7) * (diff_val >> 7)) >> coefs[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	dut = diff_val - dut;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	off = (cx_val[1] + (((cx_val[3] - 1024) * dut) >> 14)) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	sens = cx_val[0] + ((cx_val[2] * dut) >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	x = ((sens * (d1_val - 7168)) >> 14) - off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	priv->pressure = ((x * 100) >> 5) + (cx_val[6] * 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	priv->temp = 250 + ((dut * cx_val[5]) >> 16) - (dut >> coefs[17]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) err_adc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	gpiod_set_value_cansleep(priv->xclr_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int hp03_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			 struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			 int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct hp03_priv *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	mutex_lock(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	ret = hp03_update_temp_pressure(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	mutex_unlock(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		switch (chan->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		case IIO_PRESSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			*val = priv->pressure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			*val = priv->temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		switch (chan->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		case IIO_PRESSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			*val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			*val2 = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			return IIO_VAL_INT_PLUS_MICRO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			*val = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static const struct iio_info hp03_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	.read_raw	= &hp03_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static int hp03_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		      const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct hp03_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	priv->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	mutex_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	indio_dev->name = id->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	indio_dev->channels = hp03_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	indio_dev->num_channels = ARRAY_SIZE(hp03_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	indio_dev->info = &hp03_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	priv->xclr_gpio = devm_gpiod_get_index(dev, "xclr", 0, GPIOD_OUT_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (IS_ERR(priv->xclr_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		dev_err(dev, "Failed to claim XCLR GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		ret = PTR_ERR(priv->xclr_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 * Allocate another device for the on-sensor EEPROM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * which has it's dedicated I2C address and contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * the calibration constants for the sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	priv->eeprom_client = i2c_new_dummy_device(client->adapter, HP03_EEPROM_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (IS_ERR(priv->eeprom_client)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		dev_err(dev, "New EEPROM I2C device failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return PTR_ERR(priv->eeprom_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	priv->eeprom_regmap = regmap_init_i2c(priv->eeprom_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 					      &hp03_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (IS_ERR(priv->eeprom_regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		dev_err(dev, "Failed to allocate EEPROM regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ret = PTR_ERR(priv->eeprom_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		goto err_cleanup_eeprom_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	ret = iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		dev_err(dev, "Failed to register IIO device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		goto err_cleanup_eeprom_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	i2c_set_clientdata(client, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) err_cleanup_eeprom_regmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	regmap_exit(priv->eeprom_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) err_cleanup_eeprom_client:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	i2c_unregister_device(priv->eeprom_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int hp03_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct hp03_priv *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	regmap_exit(priv->eeprom_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	i2c_unregister_device(priv->eeprom_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static const struct i2c_device_id hp03_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	{ "hp03", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) MODULE_DEVICE_TABLE(i2c, hp03_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static const struct of_device_id hp03_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	{ .compatible = "hoperf,hp03" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) MODULE_DEVICE_TABLE(of, hp03_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static struct i2c_driver hp03_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		.name	= "hp03",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		.of_match_table = hp03_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	.probe		= hp03_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	.remove		= hp03_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	.id_table	= hp03_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) module_i2c_driver(hp03_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) MODULE_DESCRIPTION("Driver for Hope RF HP03 pressure and temperature sensor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_LICENSE("GPL v2");