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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *             monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *                         Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * It reports up to two temperatures (its own plus up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * one external one). Complete datasheet can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * obtained from Maxim's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static const unsigned short normal_i2c[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * The MAX1619 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define MAX1619_REG_R_MAN_ID		0xFE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define MAX1619_REG_R_CHIP_ID		0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define MAX1619_REG_R_CONFIG		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define MAX1619_REG_W_CONFIG		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define MAX1619_REG_R_CONVRATE		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define MAX1619_REG_W_CONVRATE		0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define MAX1619_REG_R_STATUS		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define MAX1619_REG_R_LOCAL_TEMP	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define MAX1619_REG_R_REMOTE_TEMP	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define MAX1619_REG_R_REMOTE_HIGH	0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define MAX1619_REG_W_REMOTE_HIGH	0x0D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define MAX1619_REG_R_REMOTE_LOW	0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define MAX1619_REG_W_REMOTE_LOW	0x0E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define MAX1619_REG_R_REMOTE_CRIT	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define MAX1619_REG_W_REMOTE_CRIT	0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define MAX1619_REG_R_TCRIT_HYST	0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define MAX1619_REG_W_TCRIT_HYST	0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * Conversions
^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 temp_from_reg(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return (val & 0x80 ? val-0x100 : val) * 1000;
^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 int temp_to_reg(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return (val < 0 ? val+0x100*1000 : val) / 1000;
^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) enum temp_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	t_input1 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	t_input2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	t_low2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	t_high2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	t_crit2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	t_hyst2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	t_num_regs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * Client data (each client gets its own)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) struct max1619_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* registers values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u8 temp[t_num_regs];	/* index with enum temp_index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u8 alarms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static const u8 regs_read[t_num_regs] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	[t_input1] = MAX1619_REG_R_LOCAL_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	[t_input2] = MAX1619_REG_R_REMOTE_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	[t_low2] = MAX1619_REG_R_REMOTE_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	[t_high2] = MAX1619_REG_R_REMOTE_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	[t_crit2] = MAX1619_REG_R_REMOTE_CRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	[t_hyst2] = MAX1619_REG_R_TCRIT_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static const u8 regs_write[t_num_regs] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	[t_low2] = MAX1619_REG_W_REMOTE_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	[t_high2] = MAX1619_REG_W_REMOTE_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	[t_crit2] = MAX1619_REG_W_REMOTE_CRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	[t_hyst2] = MAX1619_REG_W_TCRIT_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static struct max1619_data *max1619_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct max1619_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int config, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		dev_dbg(&client->dev, "Updating max1619 data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		for (i = 0; i < t_num_regs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			data->temp[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 					regs_read[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		data->alarms = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					MAX1619_REG_R_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		/* If OVERT polarity is low, reverse alarm bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (!(config & 0x20))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			data->alarms ^= 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * Sysfs stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct max1619_data *data = max1619_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static ssize_t temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			  struct device_attribute *devattr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct max1619_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	data->temp[attr->index] = temp_to_reg(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	i2c_smbus_write_byte_data(client, regs_write[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				  data->temp[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct max1619_data *data = max1619_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return sprintf(buf, "%d\n", data->alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct max1619_data *data = max1619_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, t_input2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, t_low2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, t_high2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, t_crit2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static SENSOR_DEVICE_ATTR_RW(temp2_crit_hyst, temp, t_hyst2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static struct attribute *max1619_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	&sensor_dev_attr_temp2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	&dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ATTRIBUTE_GROUPS(max1619);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int max1619_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			  struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if ((reg_config & 0x03) != 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			client->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	/* identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (man_id != 0x4D || chip_id != 0x04) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		dev_info(&adapter->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			 man_id, chip_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	strlcpy(info->type, "max1619", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static void max1619_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	u8 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	 * Start the conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				  5); /* 2 Hz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (config & 0x40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 					  config & 0xBF); /* run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int max1619_probe(struct i2c_client *new_client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct max1619_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	data = devm_kzalloc(&new_client->dev, sizeof(struct max1619_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	data->client = new_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	/* Initialize the MAX1619 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	max1619_init_client(new_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 							   new_client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 							   data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 							   max1619_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static const struct i2c_device_id max1619_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	{ "max1619", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) MODULE_DEVICE_TABLE(i2c, max1619_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static const struct of_device_id max1619_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	{ .compatible = "maxim,max1619", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	{},
^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, max1619_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static struct i2c_driver max1619_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		.name	= "max1619",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		.of_match_table = of_match_ptr(max1619_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	.probe_new	= max1619_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.id_table	= max1619_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.detect		= max1619_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) module_i2c_driver(max1619_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_DESCRIPTION("MAX1619 sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) MODULE_LICENSE("GPL");