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)  * Driver for +/-1 degree C, SMBus-Compatible Remote/Local Temperature Sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * with Overtemperature Alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2011 AppearTV AS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Derived from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  Based on the max1619 driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *                          Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * The MAX6642 is a sensor chip made by Maxim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * It reports up to two temperatures (its own plus up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * one external one). Complete datasheet can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * obtained from Maxim's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *   http://datasheets.maxim-ic.com/en/ds/MAX6642.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static const unsigned short normal_i2c[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * The MAX6642 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define MAX6642_REG_R_MAN_ID		0xFE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define MAX6642_REG_R_CONFIG		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define MAX6642_REG_W_CONFIG		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define MAX6642_REG_R_STATUS		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define MAX6642_REG_R_LOCAL_TEMP	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define MAX6642_REG_R_LOCAL_TEMPL	0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define MAX6642_REG_R_LOCAL_HIGH	0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define MAX6642_REG_W_LOCAL_HIGH	0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define MAX6642_REG_R_REMOTE_TEMP	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define MAX6642_REG_R_REMOTE_TEMPL	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define MAX6642_REG_R_REMOTE_HIGH	0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define MAX6642_REG_W_REMOTE_HIGH	0x0D
^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)  * Conversions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int temp_from_reg10(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return val * 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int temp_from_reg(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return val * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int temp_to_reg(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return val / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * Client data (each client gets its own)
^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) struct max6642_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	bool valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	/* registers values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u16 temp_input[2]; /* local/remote */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u16 temp_high[2]; /* local/remote */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u8 alarms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * Real code
^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) static void max6642_init_client(struct max6642_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u8 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * Start the conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (config & 0x40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		i2c_smbus_write_byte_data(client, MAX6642_REG_W_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 					  config & 0xBF); /* run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	data->temp_high[0] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				MAX6642_REG_R_LOCAL_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	data->temp_high[1] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				MAX6642_REG_R_REMOTE_HIGH);
^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) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int max6642_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			  struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	u8 reg_config, reg_status, man_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	man_id = i2c_smbus_read_byte_data(client, MAX6642_REG_R_MAN_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (man_id != 0x4D)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (i2c_smbus_read_byte_data(client, 0x04) != 0x4D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	    || i2c_smbus_read_byte_data(client, 0x06) != 0x4D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	    || i2c_smbus_read_byte_data(client, 0xff) != 0x4D)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * We read the config and status register, the 4 lower bits in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 * config register should be zero and bit 5, 3, 1 and 0 should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * zero in the status register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	reg_config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if ((reg_config & 0x0f) != 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* in between, another round of sanity checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (i2c_smbus_read_byte_data(client, 0x04) != reg_config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	    || i2c_smbus_read_byte_data(client, 0x06) != reg_config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	    || i2c_smbus_read_byte_data(client, 0xff) != reg_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	reg_status = i2c_smbus_read_byte_data(client, MAX6642_REG_R_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if ((reg_status & 0x2b) != 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	strlcpy(info->type, "max6642", I2C_NAME_SIZE);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static struct max6642_data *max6642_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct max6642_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	u16 val, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		dev_dbg(dev, "Updating max6642 data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		val = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 					MAX6642_REG_R_LOCAL_TEMPL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		tmp = (val >> 6) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		val = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 					MAX6642_REG_R_LOCAL_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		val = (val << 2) | tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		data->temp_input[0] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		val = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					MAX6642_REG_R_REMOTE_TEMPL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		tmp = (val >> 6) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		val = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 					MAX6642_REG_R_REMOTE_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		val = (val << 2) | tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		data->temp_input[1] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		data->alarms = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					MAX6642_REG_R_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * Sysfs stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static ssize_t temp_max10_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			       struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct max6642_data *data = max6642_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		       temp_from_reg10(data->temp_input[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static ssize_t temp_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct max6642_data *data = max6642_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static ssize_t temp_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			      struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct max6642_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	data->temp_high[attr2->nr] = clamp_val(temp_to_reg(val), 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	i2c_smbus_write_byte_data(data->client, attr2->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				  data->temp_high[attr2->nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct max6642_data *data = max6642_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
^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) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_max10, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_max10, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp_max, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			       MAX6642_REG_W_LOCAL_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp_max, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			       MAX6642_REG_W_REMOTE_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static struct attribute *max6642_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ATTRIBUTE_GROUPS(max6642);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int max6642_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct max6642_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	data = devm_kzalloc(dev, sizeof(struct max6642_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/* Initialize the MAX6642 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	max6642_init_client(data, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 							   client->name, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 							   max6642_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * Driver data (common to all clients)
^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) static const struct i2c_device_id max6642_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	{ "max6642", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) MODULE_DEVICE_TABLE(i2c, max6642_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static struct i2c_driver max6642_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		.name	= "max6642",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	.probe_new	= max6642_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	.id_table	= max6642_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	.detect		= max6642_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) module_i2c_driver(max6642_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) MODULE_DESCRIPTION("MAX6642 sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_LICENSE("GPL");