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)  * An hwmon driver for the Analog Devices AD7416/17/18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2006-07 Tower Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Alessandro Zummo <a.zummo@towertech.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on lm75.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "lm75.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define DRV_VERSION "0.4"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) enum chips { ad7416, ad7417, ad7418 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* AD7418 registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define AD7418_REG_TEMP_IN	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define AD7418_REG_CONF		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define AD7418_REG_TEMP_HYST	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define AD7418_REG_TEMP_OS	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define AD7418_REG_ADC		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define AD7418_REG_CONF2	0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define AD7418_REG_ADC_CH(x)	((x) << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define AD7418_CH_TEMP		AD7418_REG_ADC_CH(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 					AD7418_REG_TEMP_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 					AD7418_REG_TEMP_OS };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) struct ad7418_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	enum chips		type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct mutex		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int			adc_max;	/* number of ADC channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	char			valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned long		last_updated;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	s16			temp[3];	/* Register values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u16			in[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int ad7418_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct ad7418_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	s32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		|| !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		u8 cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		int i, ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		/* read config register and clear channel bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		val = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		cfg = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		cfg &= 0x1F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 						cfg | AD7418_CH_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		udelay(30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			val = i2c_smbus_read_word_swapped(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 							  AD7418_REG_TEMP[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			data->temp[i] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 					cfg | AD7418_REG_ADC_CH(ch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			udelay(15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			val = i2c_smbus_read_word_swapped(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 							  AD7418_REG_ADC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			data->in[data->adc_max - 1 - i] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		/* restore old configuration value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		val = i2c_smbus_write_word_swapped(client, AD7418_REG_CONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 						   cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	data->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct ad7418_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ret = ad7418_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		LM75_TEMP_FROM_REG(data->temp[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct ad7418_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	ret = ad7418_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static ssize_t temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			  struct device_attribute *devattr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct ad7418_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	long temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int ret = kstrtol(buf, 10, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	i2c_smbus_write_word_swapped(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				     AD7418_REG_TEMP[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				     data->temp[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static struct attribute *ad7416_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ATTRIBUTE_GROUPS(ad7416);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static struct attribute *ad7417_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	&sensor_dev_attr_in3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	&sensor_dev_attr_in4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ATTRIBUTE_GROUPS(ad7417);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static struct attribute *ad7418_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ATTRIBUTE_GROUPS(ad7418);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void ad7418_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct ad7418_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (reg < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		dev_err(&client->dev, "cannot read configuration register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		dev_info(&client->dev, "configuring for mode 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (data->type == ad7417 || data->type == ad7418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 						AD7418_REG_CONF2, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static const struct i2c_device_id ad7418_id[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int ad7418_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct ad7418_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	const struct attribute_group **attr_groups = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 					I2C_FUNC_SMBUS_WORD_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (dev->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		data->type = (enum chips)of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		data->type = i2c_match_id(ad7418_id, client)->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	switch (data->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	case ad7416:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		data->adc_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		attr_groups = ad7416_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	case ad7417:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		data->adc_max = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		attr_groups = ad7417_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	case ad7418:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		data->adc_max = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		attr_groups = ad7418_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		break;
^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) 	dev_info(dev, "%s chip found\n", client->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	/* Initialize the AD7418 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	ad7418_init_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 							   client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 							   data, attr_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static const struct i2c_device_id ad7418_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	{ "ad7416", ad7416 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	{ "ad7417", ad7417 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	{ "ad7418", ad7418 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) MODULE_DEVICE_TABLE(i2c, ad7418_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static const struct of_device_id ad7418_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	{ .compatible = "adi,ad7416", .data = (void *)ad7416, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	{ .compatible = "adi,ad7417", .data = (void *)ad7417, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	{ .compatible = "adi,ad7418", .data = (void *)ad7418, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) MODULE_DEVICE_TABLE(of, ad7418_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static struct i2c_driver ad7418_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		.name	= "ad7418",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		.of_match_table = ad7418_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.probe_new	= ad7418_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.id_table	= ad7418_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) module_i2c_driver(ad7418_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_DESCRIPTION("AD7416/17/18 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) MODULE_VERSION(DRV_VERSION);