18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * lm63.c - driver for the National Semiconductor LM63 temperature sensor 48c2ecf20Sopenharmony_ci * with integrated fan control 58c2ecf20Sopenharmony_ci * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de> 68c2ecf20Sopenharmony_ci * Based on the lm90 driver. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * The LM63 is a sensor chip made by National Semiconductor. It measures 98c2ecf20Sopenharmony_ci * two temperatures (its own and one external one) and the speed of one 108c2ecf20Sopenharmony_ci * fan, those speed it can additionally control. Complete datasheet can be 118c2ecf20Sopenharmony_ci * obtained from National's website at: 128c2ecf20Sopenharmony_ci * http://www.national.com/pf/LM/LM63.html 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * The LM63 is basically an LM86 with fan speed monitoring and control 158c2ecf20Sopenharmony_ci * capabilities added. It misses some of the LM86 features though: 168c2ecf20Sopenharmony_ci * - No low limit for local temperature. 178c2ecf20Sopenharmony_ci * - No critical limit for local temperature. 188c2ecf20Sopenharmony_ci * - Critical limit for remote temperature can be changed only once. We 198c2ecf20Sopenharmony_ci * will consider that the critical limit is read-only. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * The datasheet isn't very clear about what the tachometer reading is. 228c2ecf20Sopenharmony_ci * I had a explanation from National Semiconductor though. The two lower 238c2ecf20Sopenharmony_ci * bits of the read value have to be masked out. The value is still 16 bit 248c2ecf20Sopenharmony_ci * in width. 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include <linux/module.h> 288c2ecf20Sopenharmony_ci#include <linux/init.h> 298c2ecf20Sopenharmony_ci#include <linux/slab.h> 308c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 318c2ecf20Sopenharmony_ci#include <linux/i2c.h> 328c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 338c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 348c2ecf20Sopenharmony_ci#include <linux/err.h> 358c2ecf20Sopenharmony_ci#include <linux/mutex.h> 368c2ecf20Sopenharmony_ci#include <linux/of_device.h> 378c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 388c2ecf20Sopenharmony_ci#include <linux/types.h> 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* 418c2ecf20Sopenharmony_ci * Addresses to scan 428c2ecf20Sopenharmony_ci * Address is fully defined internally and cannot be changed except for 438c2ecf20Sopenharmony_ci * LM64 which has one pin dedicated to address selection. 448c2ecf20Sopenharmony_ci * LM63 and LM96163 have address 0x4c. 458c2ecf20Sopenharmony_ci * LM64 can have address 0x18 or 0x4e. 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * The LM63 registers 528c2ecf20Sopenharmony_ci */ 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define LM63_REG_CONFIG1 0x03 558c2ecf20Sopenharmony_ci#define LM63_REG_CONVRATE 0x04 568c2ecf20Sopenharmony_ci#define LM63_REG_CONFIG2 0xBF 578c2ecf20Sopenharmony_ci#define LM63_REG_CONFIG_FAN 0x4A 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci#define LM63_REG_TACH_COUNT_MSB 0x47 608c2ecf20Sopenharmony_ci#define LM63_REG_TACH_COUNT_LSB 0x46 618c2ecf20Sopenharmony_ci#define LM63_REG_TACH_LIMIT_MSB 0x49 628c2ecf20Sopenharmony_ci#define LM63_REG_TACH_LIMIT_LSB 0x48 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define LM63_REG_PWM_VALUE 0x4C 658c2ecf20Sopenharmony_ci#define LM63_REG_PWM_FREQ 0x4D 668c2ecf20Sopenharmony_ci#define LM63_REG_LUT_TEMP_HYST 0x4F 678c2ecf20Sopenharmony_ci#define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr)) 688c2ecf20Sopenharmony_ci#define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr)) 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci#define LM63_REG_LOCAL_TEMP 0x00 718c2ecf20Sopenharmony_ci#define LM63_REG_LOCAL_HIGH 0x05 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_TEMP_MSB 0x01 748c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_TEMP_LSB 0x10 758c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_OFFSET_MSB 0x11 768c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_OFFSET_LSB 0x12 778c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_HIGH_MSB 0x07 788c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_HIGH_LSB 0x13 798c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_LOW_MSB 0x08 808c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_LOW_LSB 0x14 818c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_TCRIT 0x19 828c2ecf20Sopenharmony_ci#define LM63_REG_REMOTE_TCRIT_HYST 0x21 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci#define LM63_REG_ALERT_STATUS 0x02 858c2ecf20Sopenharmony_ci#define LM63_REG_ALERT_MASK 0x16 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci#define LM63_REG_MAN_ID 0xFE 888c2ecf20Sopenharmony_ci#define LM63_REG_CHIP_ID 0xFF 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define LM96163_REG_TRUTHERM 0x30 918c2ecf20Sopenharmony_ci#define LM96163_REG_REMOTE_TEMP_U_MSB 0x31 928c2ecf20Sopenharmony_ci#define LM96163_REG_REMOTE_TEMP_U_LSB 0x32 938c2ecf20Sopenharmony_ci#define LM96163_REG_CONFIG_ENHANCED 0x45 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci#define LM63_MAX_CONVRATE 9 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci#define LM63_MAX_CONVRATE_HZ 32 988c2ecf20Sopenharmony_ci#define LM96163_MAX_CONVRATE_HZ 26 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* 1018c2ecf20Sopenharmony_ci * Conversions and various macros 1028c2ecf20Sopenharmony_ci * For tachometer counts, the LM63 uses 16-bit values. 1038c2ecf20Sopenharmony_ci * For local temperature and high limit, remote critical limit and hysteresis 1048c2ecf20Sopenharmony_ci * value, it uses signed 8-bit values with LSB = 1 degree Celsius. 1058c2ecf20Sopenharmony_ci * For remote temperature, low and high limits, it uses signed 11-bit values 1068c2ecf20Sopenharmony_ci * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. 1078c2ecf20Sopenharmony_ci * For LM64 the actual remote diode temperature is 16 degree Celsius higher 1088c2ecf20Sopenharmony_ci * than the register reading. Remote temperature setpoints have to be 1098c2ecf20Sopenharmony_ci * adapted accordingly. 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ 1138c2ecf20Sopenharmony_ci 5400000 / (reg)) 1148c2ecf20Sopenharmony_ci#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ 1158c2ecf20Sopenharmony_ci (5400000 / (val)) & 0xFFFC) 1168c2ecf20Sopenharmony_ci#define TEMP8_FROM_REG(reg) ((reg) * 1000) 1178c2ecf20Sopenharmony_ci#define TEMP8_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), -128000, \ 1188c2ecf20Sopenharmony_ci 127000), 1000) 1198c2ecf20Sopenharmony_ci#define TEMP8U_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, \ 1208c2ecf20Sopenharmony_ci 255000), 1000) 1218c2ecf20Sopenharmony_ci#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) 1228c2ecf20Sopenharmony_ci#define TEMP11_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), -128000, \ 1238c2ecf20Sopenharmony_ci 127875), 125) * 32) 1248c2ecf20Sopenharmony_ci#define TEMP11U_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), 0, \ 1258c2ecf20Sopenharmony_ci 255875), 125) * 32) 1268c2ecf20Sopenharmony_ci#define HYST_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 127000), \ 1278c2ecf20Sopenharmony_ci 1000) 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci#define UPDATE_INTERVAL(max, rate) \ 1308c2ecf20Sopenharmony_ci ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max)) 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cienum chips { lm63, lm64, lm96163 }; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci/* 1358c2ecf20Sopenharmony_ci * Client data (each client gets its own) 1368c2ecf20Sopenharmony_ci */ 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistruct lm63_data { 1398c2ecf20Sopenharmony_ci struct i2c_client *client; 1408c2ecf20Sopenharmony_ci struct mutex update_lock; 1418c2ecf20Sopenharmony_ci const struct attribute_group *groups[5]; 1428c2ecf20Sopenharmony_ci char valid; /* zero until following fields are valid */ 1438c2ecf20Sopenharmony_ci char lut_valid; /* zero until lut fields are valid */ 1448c2ecf20Sopenharmony_ci unsigned long last_updated; /* in jiffies */ 1458c2ecf20Sopenharmony_ci unsigned long lut_last_updated; /* in jiffies */ 1468c2ecf20Sopenharmony_ci enum chips kind; 1478c2ecf20Sopenharmony_ci int temp2_offset; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci int update_interval; /* in milliseconds */ 1508c2ecf20Sopenharmony_ci int max_convrate_hz; 1518c2ecf20Sopenharmony_ci int lut_size; /* 8 or 12 */ 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* registers values */ 1548c2ecf20Sopenharmony_ci u8 config, config_fan; 1558c2ecf20Sopenharmony_ci u16 fan[2]; /* 0: input 1568c2ecf20Sopenharmony_ci 1: low limit */ 1578c2ecf20Sopenharmony_ci u8 pwm1_freq; 1588c2ecf20Sopenharmony_ci u8 pwm1[13]; /* 0: current output 1598c2ecf20Sopenharmony_ci 1-12: lookup table */ 1608c2ecf20Sopenharmony_ci s8 temp8[15]; /* 0: local input 1618c2ecf20Sopenharmony_ci 1: local high limit 1628c2ecf20Sopenharmony_ci 2: remote critical limit 1638c2ecf20Sopenharmony_ci 3-14: lookup table */ 1648c2ecf20Sopenharmony_ci s16 temp11[4]; /* 0: remote input 1658c2ecf20Sopenharmony_ci 1: remote low limit 1668c2ecf20Sopenharmony_ci 2: remote high limit 1678c2ecf20Sopenharmony_ci 3: remote offset */ 1688c2ecf20Sopenharmony_ci u16 temp11u; /* remote input (unsigned) */ 1698c2ecf20Sopenharmony_ci u8 temp2_crit_hyst; 1708c2ecf20Sopenharmony_ci u8 lut_temp_hyst; 1718c2ecf20Sopenharmony_ci u8 alarms; 1728c2ecf20Sopenharmony_ci bool pwm_highres; 1738c2ecf20Sopenharmony_ci bool lut_temp_highres; 1748c2ecf20Sopenharmony_ci bool remote_unsigned; /* true if unsigned remote upper limits */ 1758c2ecf20Sopenharmony_ci bool trutherm; 1768c2ecf20Sopenharmony_ci}; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cistatic inline int temp8_from_reg(struct lm63_data *data, int nr) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci if (data->remote_unsigned) 1818c2ecf20Sopenharmony_ci return TEMP8_FROM_REG((u8)data->temp8[nr]); 1828c2ecf20Sopenharmony_ci return TEMP8_FROM_REG(data->temp8[nr]); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic inline int lut_temp_from_reg(struct lm63_data *data, int nr) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000); 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic inline int lut_temp_to_reg(struct lm63_data *data, long val) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci val -= data->temp2_offset; 1938c2ecf20Sopenharmony_ci if (data->lut_temp_highres) 1948c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127500), 500); 1958c2ecf20Sopenharmony_ci else 1968c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000); 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci/* 2008c2ecf20Sopenharmony_ci * Update the lookup table register cache. 2018c2ecf20Sopenharmony_ci * client->update_lock must be held when calling this function. 2028c2ecf20Sopenharmony_ci */ 2038c2ecf20Sopenharmony_cistatic void lm63_update_lut(struct lm63_data *data) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 2068c2ecf20Sopenharmony_ci int i; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (time_after(jiffies, data->lut_last_updated + 5 * HZ) || 2098c2ecf20Sopenharmony_ci !data->lut_valid) { 2108c2ecf20Sopenharmony_ci for (i = 0; i < data->lut_size; i++) { 2118c2ecf20Sopenharmony_ci data->pwm1[1 + i] = i2c_smbus_read_byte_data(client, 2128c2ecf20Sopenharmony_ci LM63_REG_LUT_PWM(i)); 2138c2ecf20Sopenharmony_ci data->temp8[3 + i] = i2c_smbus_read_byte_data(client, 2148c2ecf20Sopenharmony_ci LM63_REG_LUT_TEMP(i)); 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci data->lut_temp_hyst = i2c_smbus_read_byte_data(client, 2178c2ecf20Sopenharmony_ci LM63_REG_LUT_TEMP_HYST); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci data->lut_last_updated = jiffies; 2208c2ecf20Sopenharmony_ci data->lut_valid = 1; 2218c2ecf20Sopenharmony_ci } 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic struct lm63_data *lm63_update_device(struct device *dev) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 2278c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 2288c2ecf20Sopenharmony_ci unsigned long next_update; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci next_update = data->last_updated + 2338c2ecf20Sopenharmony_ci msecs_to_jiffies(data->update_interval); 2348c2ecf20Sopenharmony_ci if (time_after(jiffies, next_update) || !data->valid) { 2358c2ecf20Sopenharmony_ci if (data->config & 0x04) { /* tachometer enabled */ 2368c2ecf20Sopenharmony_ci /* order matters for fan1_input */ 2378c2ecf20Sopenharmony_ci data->fan[0] = i2c_smbus_read_byte_data(client, 2388c2ecf20Sopenharmony_ci LM63_REG_TACH_COUNT_LSB) & 0xFC; 2398c2ecf20Sopenharmony_ci data->fan[0] |= i2c_smbus_read_byte_data(client, 2408c2ecf20Sopenharmony_ci LM63_REG_TACH_COUNT_MSB) << 8; 2418c2ecf20Sopenharmony_ci data->fan[1] = (i2c_smbus_read_byte_data(client, 2428c2ecf20Sopenharmony_ci LM63_REG_TACH_LIMIT_LSB) & 0xFC) 2438c2ecf20Sopenharmony_ci | (i2c_smbus_read_byte_data(client, 2448c2ecf20Sopenharmony_ci LM63_REG_TACH_LIMIT_MSB) << 8); 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci data->pwm1_freq = i2c_smbus_read_byte_data(client, 2488c2ecf20Sopenharmony_ci LM63_REG_PWM_FREQ); 2498c2ecf20Sopenharmony_ci if (data->pwm1_freq == 0) 2508c2ecf20Sopenharmony_ci data->pwm1_freq = 1; 2518c2ecf20Sopenharmony_ci data->pwm1[0] = i2c_smbus_read_byte_data(client, 2528c2ecf20Sopenharmony_ci LM63_REG_PWM_VALUE); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci data->temp8[0] = i2c_smbus_read_byte_data(client, 2558c2ecf20Sopenharmony_ci LM63_REG_LOCAL_TEMP); 2568c2ecf20Sopenharmony_ci data->temp8[1] = i2c_smbus_read_byte_data(client, 2578c2ecf20Sopenharmony_ci LM63_REG_LOCAL_HIGH); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* order matters for temp2_input */ 2608c2ecf20Sopenharmony_ci data->temp11[0] = i2c_smbus_read_byte_data(client, 2618c2ecf20Sopenharmony_ci LM63_REG_REMOTE_TEMP_MSB) << 8; 2628c2ecf20Sopenharmony_ci data->temp11[0] |= i2c_smbus_read_byte_data(client, 2638c2ecf20Sopenharmony_ci LM63_REG_REMOTE_TEMP_LSB); 2648c2ecf20Sopenharmony_ci data->temp11[1] = (i2c_smbus_read_byte_data(client, 2658c2ecf20Sopenharmony_ci LM63_REG_REMOTE_LOW_MSB) << 8) 2668c2ecf20Sopenharmony_ci | i2c_smbus_read_byte_data(client, 2678c2ecf20Sopenharmony_ci LM63_REG_REMOTE_LOW_LSB); 2688c2ecf20Sopenharmony_ci data->temp11[2] = (i2c_smbus_read_byte_data(client, 2698c2ecf20Sopenharmony_ci LM63_REG_REMOTE_HIGH_MSB) << 8) 2708c2ecf20Sopenharmony_ci | i2c_smbus_read_byte_data(client, 2718c2ecf20Sopenharmony_ci LM63_REG_REMOTE_HIGH_LSB); 2728c2ecf20Sopenharmony_ci data->temp11[3] = (i2c_smbus_read_byte_data(client, 2738c2ecf20Sopenharmony_ci LM63_REG_REMOTE_OFFSET_MSB) << 8) 2748c2ecf20Sopenharmony_ci | i2c_smbus_read_byte_data(client, 2758c2ecf20Sopenharmony_ci LM63_REG_REMOTE_OFFSET_LSB); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci if (data->kind == lm96163) 2788c2ecf20Sopenharmony_ci data->temp11u = (i2c_smbus_read_byte_data(client, 2798c2ecf20Sopenharmony_ci LM96163_REG_REMOTE_TEMP_U_MSB) << 8) 2808c2ecf20Sopenharmony_ci | i2c_smbus_read_byte_data(client, 2818c2ecf20Sopenharmony_ci LM96163_REG_REMOTE_TEMP_U_LSB); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci data->temp8[2] = i2c_smbus_read_byte_data(client, 2848c2ecf20Sopenharmony_ci LM63_REG_REMOTE_TCRIT); 2858c2ecf20Sopenharmony_ci data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, 2868c2ecf20Sopenharmony_ci LM63_REG_REMOTE_TCRIT_HYST); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci data->alarms = i2c_smbus_read_byte_data(client, 2898c2ecf20Sopenharmony_ci LM63_REG_ALERT_STATUS) & 0x7F; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci data->last_updated = jiffies; 2928c2ecf20Sopenharmony_ci data->valid = 1; 2938c2ecf20Sopenharmony_ci } 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci lm63_update_lut(data); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci return data; 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci/* 3038c2ecf20Sopenharmony_ci * Trip points in the lookup table should be in ascending order for both 3048c2ecf20Sopenharmony_ci * temperatures and PWM output values. 3058c2ecf20Sopenharmony_ci */ 3068c2ecf20Sopenharmony_cistatic int lm63_lut_looks_bad(struct device *dev, struct lm63_data *data) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci int i; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 3118c2ecf20Sopenharmony_ci lm63_update_lut(data); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci for (i = 1; i < data->lut_size; i++) { 3148c2ecf20Sopenharmony_ci if (data->pwm1[1 + i - 1] > data->pwm1[1 + i] 3158c2ecf20Sopenharmony_ci || data->temp8[3 + i - 1] > data->temp8[3 + i]) { 3168c2ecf20Sopenharmony_ci dev_warn(dev, 3178c2ecf20Sopenharmony_ci "Lookup table doesn't look sane (check entries %d and %d)\n", 3188c2ecf20Sopenharmony_ci i, i + 1); 3198c2ecf20Sopenharmony_ci break; 3208c2ecf20Sopenharmony_ci } 3218c2ecf20Sopenharmony_ci } 3228c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci return i == data->lut_size ? 0 : 1; 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci/* 3288c2ecf20Sopenharmony_ci * Sysfs callback functions and files 3298c2ecf20Sopenharmony_ci */ 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_cistatic ssize_t show_fan(struct device *dev, struct device_attribute *devattr, 3328c2ecf20Sopenharmony_ci char *buf) 3338c2ecf20Sopenharmony_ci{ 3348c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3358c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 3368c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cistatic ssize_t set_fan(struct device *dev, struct device_attribute *dummy, 3408c2ecf20Sopenharmony_ci const char *buf, size_t count) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 3438c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 3448c2ecf20Sopenharmony_ci unsigned long val; 3458c2ecf20Sopenharmony_ci int err; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 3488c2ecf20Sopenharmony_ci if (err) 3498c2ecf20Sopenharmony_ci return err; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 3528c2ecf20Sopenharmony_ci data->fan[1] = FAN_TO_REG(val); 3538c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, 3548c2ecf20Sopenharmony_ci data->fan[1] & 0xFF); 3558c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, 3568c2ecf20Sopenharmony_ci data->fan[1] >> 8); 3578c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 3588c2ecf20Sopenharmony_ci return count; 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_cistatic ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr, 3628c2ecf20Sopenharmony_ci char *buf) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3658c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 3668c2ecf20Sopenharmony_ci int nr = attr->index; 3678c2ecf20Sopenharmony_ci int pwm; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci if (data->pwm_highres) 3708c2ecf20Sopenharmony_ci pwm = data->pwm1[nr]; 3718c2ecf20Sopenharmony_ci else 3728c2ecf20Sopenharmony_ci pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ? 3738c2ecf20Sopenharmony_ci 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) / 3748c2ecf20Sopenharmony_ci (2 * data->pwm1_freq); 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", pwm); 3778c2ecf20Sopenharmony_ci} 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_cistatic ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr, 3808c2ecf20Sopenharmony_ci const char *buf, size_t count) 3818c2ecf20Sopenharmony_ci{ 3828c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3838c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 3848c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 3858c2ecf20Sopenharmony_ci int nr = attr->index; 3868c2ecf20Sopenharmony_ci unsigned long val; 3878c2ecf20Sopenharmony_ci int err; 3888c2ecf20Sopenharmony_ci u8 reg; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci if (!(data->config_fan & 0x20)) /* register is read-only */ 3918c2ecf20Sopenharmony_ci return -EPERM; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 3948c2ecf20Sopenharmony_ci if (err) 3958c2ecf20Sopenharmony_ci return err; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE; 3988c2ecf20Sopenharmony_ci val = clamp_val(val, 0, 255); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4018c2ecf20Sopenharmony_ci data->pwm1[nr] = data->pwm_highres ? val : 4028c2ecf20Sopenharmony_ci (val * data->pwm1_freq * 2 + 127) / 255; 4038c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]); 4048c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4058c2ecf20Sopenharmony_ci return count; 4068c2ecf20Sopenharmony_ci} 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_cistatic ssize_t pwm1_enable_show(struct device *dev, 4098c2ecf20Sopenharmony_ci struct device_attribute *dummy, char *buf) 4108c2ecf20Sopenharmony_ci{ 4118c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 4128c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); 4138c2ecf20Sopenharmony_ci} 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_cistatic ssize_t pwm1_enable_store(struct device *dev, 4168c2ecf20Sopenharmony_ci struct device_attribute *dummy, 4178c2ecf20Sopenharmony_ci const char *buf, size_t count) 4188c2ecf20Sopenharmony_ci{ 4198c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 4208c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 4218c2ecf20Sopenharmony_ci unsigned long val; 4228c2ecf20Sopenharmony_ci int err; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 4258c2ecf20Sopenharmony_ci if (err) 4268c2ecf20Sopenharmony_ci return err; 4278c2ecf20Sopenharmony_ci if (val < 1 || val > 2) 4288c2ecf20Sopenharmony_ci return -EINVAL; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci /* 4318c2ecf20Sopenharmony_ci * Only let the user switch to automatic mode if the lookup table 4328c2ecf20Sopenharmony_ci * looks sane. 4338c2ecf20Sopenharmony_ci */ 4348c2ecf20Sopenharmony_ci if (val == 2 && lm63_lut_looks_bad(dev, data)) 4358c2ecf20Sopenharmony_ci return -EPERM; 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4388c2ecf20Sopenharmony_ci data->config_fan = i2c_smbus_read_byte_data(client, 4398c2ecf20Sopenharmony_ci LM63_REG_CONFIG_FAN); 4408c2ecf20Sopenharmony_ci if (val == 1) 4418c2ecf20Sopenharmony_ci data->config_fan |= 0x20; 4428c2ecf20Sopenharmony_ci else 4438c2ecf20Sopenharmony_ci data->config_fan &= ~0x20; 4448c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, 4458c2ecf20Sopenharmony_ci data->config_fan); 4468c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4478c2ecf20Sopenharmony_ci return count; 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci/* 4518c2ecf20Sopenharmony_ci * There are 8bit registers for both local(temp1) and remote(temp2) sensor. 4528c2ecf20Sopenharmony_ci * For remote sensor registers temp2_offset has to be considered, 4538c2ecf20Sopenharmony_ci * for local sensor it must not. 4548c2ecf20Sopenharmony_ci * So we need separate 8bit accessors for local and remote sensor. 4558c2ecf20Sopenharmony_ci */ 4568c2ecf20Sopenharmony_cistatic ssize_t show_local_temp8(struct device *dev, 4578c2ecf20Sopenharmony_ci struct device_attribute *devattr, 4588c2ecf20Sopenharmony_ci char *buf) 4598c2ecf20Sopenharmony_ci{ 4608c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4618c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 4628c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); 4638c2ecf20Sopenharmony_ci} 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_cistatic ssize_t show_remote_temp8(struct device *dev, 4668c2ecf20Sopenharmony_ci struct device_attribute *devattr, 4678c2ecf20Sopenharmony_ci char *buf) 4688c2ecf20Sopenharmony_ci{ 4698c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4708c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 4718c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index) 4728c2ecf20Sopenharmony_ci + data->temp2_offset); 4738c2ecf20Sopenharmony_ci} 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_cistatic ssize_t show_lut_temp(struct device *dev, 4768c2ecf20Sopenharmony_ci struct device_attribute *devattr, 4778c2ecf20Sopenharmony_ci char *buf) 4788c2ecf20Sopenharmony_ci{ 4798c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4808c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 4818c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index) 4828c2ecf20Sopenharmony_ci + data->temp2_offset); 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_cistatic ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, 4868c2ecf20Sopenharmony_ci const char *buf, size_t count) 4878c2ecf20Sopenharmony_ci{ 4888c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4898c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 4908c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 4918c2ecf20Sopenharmony_ci int nr = attr->index; 4928c2ecf20Sopenharmony_ci long val; 4938c2ecf20Sopenharmony_ci int err; 4948c2ecf20Sopenharmony_ci int temp; 4958c2ecf20Sopenharmony_ci u8 reg; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 4988c2ecf20Sopenharmony_ci if (err) 4998c2ecf20Sopenharmony_ci return err; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5028c2ecf20Sopenharmony_ci switch (nr) { 5038c2ecf20Sopenharmony_ci case 2: 5048c2ecf20Sopenharmony_ci reg = LM63_REG_REMOTE_TCRIT; 5058c2ecf20Sopenharmony_ci if (data->remote_unsigned) 5068c2ecf20Sopenharmony_ci temp = TEMP8U_TO_REG(val - data->temp2_offset); 5078c2ecf20Sopenharmony_ci else 5088c2ecf20Sopenharmony_ci temp = TEMP8_TO_REG(val - data->temp2_offset); 5098c2ecf20Sopenharmony_ci break; 5108c2ecf20Sopenharmony_ci case 1: 5118c2ecf20Sopenharmony_ci reg = LM63_REG_LOCAL_HIGH; 5128c2ecf20Sopenharmony_ci temp = TEMP8_TO_REG(val); 5138c2ecf20Sopenharmony_ci break; 5148c2ecf20Sopenharmony_ci default: /* lookup table */ 5158c2ecf20Sopenharmony_ci reg = LM63_REG_LUT_TEMP(nr - 3); 5168c2ecf20Sopenharmony_ci temp = lut_temp_to_reg(data, val); 5178c2ecf20Sopenharmony_ci } 5188c2ecf20Sopenharmony_ci data->temp8[nr] = temp; 5198c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, reg, temp); 5208c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 5218c2ecf20Sopenharmony_ci return count; 5228c2ecf20Sopenharmony_ci} 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_cistatic ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, 5258c2ecf20Sopenharmony_ci char *buf) 5268c2ecf20Sopenharmony_ci{ 5278c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 5288c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 5298c2ecf20Sopenharmony_ci int nr = attr->index; 5308c2ecf20Sopenharmony_ci int temp; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci if (!nr) { 5338c2ecf20Sopenharmony_ci /* 5348c2ecf20Sopenharmony_ci * Use unsigned temperature unless its value is zero. 5358c2ecf20Sopenharmony_ci * If it is zero, use signed temperature. 5368c2ecf20Sopenharmony_ci */ 5378c2ecf20Sopenharmony_ci if (data->temp11u) 5388c2ecf20Sopenharmony_ci temp = TEMP11_FROM_REG(data->temp11u); 5398c2ecf20Sopenharmony_ci else 5408c2ecf20Sopenharmony_ci temp = TEMP11_FROM_REG(data->temp11[nr]); 5418c2ecf20Sopenharmony_ci } else { 5428c2ecf20Sopenharmony_ci if (data->remote_unsigned && nr == 2) 5438c2ecf20Sopenharmony_ci temp = TEMP11_FROM_REG((u16)data->temp11[nr]); 5448c2ecf20Sopenharmony_ci else 5458c2ecf20Sopenharmony_ci temp = TEMP11_FROM_REG(data->temp11[nr]); 5468c2ecf20Sopenharmony_ci } 5478c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", temp + data->temp2_offset); 5488c2ecf20Sopenharmony_ci} 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_cistatic ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, 5518c2ecf20Sopenharmony_ci const char *buf, size_t count) 5528c2ecf20Sopenharmony_ci{ 5538c2ecf20Sopenharmony_ci static const u8 reg[6] = { 5548c2ecf20Sopenharmony_ci LM63_REG_REMOTE_LOW_MSB, 5558c2ecf20Sopenharmony_ci LM63_REG_REMOTE_LOW_LSB, 5568c2ecf20Sopenharmony_ci LM63_REG_REMOTE_HIGH_MSB, 5578c2ecf20Sopenharmony_ci LM63_REG_REMOTE_HIGH_LSB, 5588c2ecf20Sopenharmony_ci LM63_REG_REMOTE_OFFSET_MSB, 5598c2ecf20Sopenharmony_ci LM63_REG_REMOTE_OFFSET_LSB, 5608c2ecf20Sopenharmony_ci }; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 5638c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 5648c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 5658c2ecf20Sopenharmony_ci long val; 5668c2ecf20Sopenharmony_ci int err; 5678c2ecf20Sopenharmony_ci int nr = attr->index; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 5708c2ecf20Sopenharmony_ci if (err) 5718c2ecf20Sopenharmony_ci return err; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5748c2ecf20Sopenharmony_ci if (data->remote_unsigned && nr == 2) 5758c2ecf20Sopenharmony_ci data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset); 5768c2ecf20Sopenharmony_ci else 5778c2ecf20Sopenharmony_ci data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset); 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], 5808c2ecf20Sopenharmony_ci data->temp11[nr] >> 8); 5818c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], 5828c2ecf20Sopenharmony_ci data->temp11[nr] & 0xff); 5838c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 5848c2ecf20Sopenharmony_ci return count; 5858c2ecf20Sopenharmony_ci} 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci/* 5888c2ecf20Sopenharmony_ci * Hysteresis register holds a relative value, while we want to present 5898c2ecf20Sopenharmony_ci * an absolute to user-space 5908c2ecf20Sopenharmony_ci */ 5918c2ecf20Sopenharmony_cistatic ssize_t temp2_crit_hyst_show(struct device *dev, 5928c2ecf20Sopenharmony_ci struct device_attribute *dummy, char *buf) 5938c2ecf20Sopenharmony_ci{ 5948c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 5958c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", temp8_from_reg(data, 2) 5968c2ecf20Sopenharmony_ci + data->temp2_offset 5978c2ecf20Sopenharmony_ci - TEMP8_FROM_REG(data->temp2_crit_hyst)); 5988c2ecf20Sopenharmony_ci} 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_cistatic ssize_t show_lut_temp_hyst(struct device *dev, 6018c2ecf20Sopenharmony_ci struct device_attribute *devattr, char *buf) 6028c2ecf20Sopenharmony_ci{ 6038c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 6048c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index) 6078c2ecf20Sopenharmony_ci + data->temp2_offset 6088c2ecf20Sopenharmony_ci - TEMP8_FROM_REG(data->lut_temp_hyst)); 6098c2ecf20Sopenharmony_ci} 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci/* 6128c2ecf20Sopenharmony_ci * And now the other way around, user-space provides an absolute 6138c2ecf20Sopenharmony_ci * hysteresis value and we have to store a relative one 6148c2ecf20Sopenharmony_ci */ 6158c2ecf20Sopenharmony_cistatic ssize_t temp2_crit_hyst_store(struct device *dev, 6168c2ecf20Sopenharmony_ci struct device_attribute *dummy, 6178c2ecf20Sopenharmony_ci const char *buf, size_t count) 6188c2ecf20Sopenharmony_ci{ 6198c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 6208c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 6218c2ecf20Sopenharmony_ci long val; 6228c2ecf20Sopenharmony_ci int err; 6238c2ecf20Sopenharmony_ci long hyst; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 6268c2ecf20Sopenharmony_ci if (err) 6278c2ecf20Sopenharmony_ci return err; 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6308c2ecf20Sopenharmony_ci hyst = temp8_from_reg(data, 2) + data->temp2_offset - val; 6318c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, 6328c2ecf20Sopenharmony_ci HYST_TO_REG(hyst)); 6338c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6348c2ecf20Sopenharmony_ci return count; 6358c2ecf20Sopenharmony_ci} 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci/* 6388c2ecf20Sopenharmony_ci * Set conversion rate. 6398c2ecf20Sopenharmony_ci * client->update_lock must be held when calling this function. 6408c2ecf20Sopenharmony_ci */ 6418c2ecf20Sopenharmony_cistatic void lm63_set_convrate(struct lm63_data *data, unsigned int interval) 6428c2ecf20Sopenharmony_ci{ 6438c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 6448c2ecf20Sopenharmony_ci unsigned int update_interval; 6458c2ecf20Sopenharmony_ci int i; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci /* Shift calculations to avoid rounding errors */ 6488c2ecf20Sopenharmony_ci interval <<= 6; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci /* find the nearest update rate */ 6518c2ecf20Sopenharmony_ci update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000 6528c2ecf20Sopenharmony_ci / data->max_convrate_hz; 6538c2ecf20Sopenharmony_ci for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1) 6548c2ecf20Sopenharmony_ci if (interval >= update_interval * 3 / 4) 6558c2ecf20Sopenharmony_ci break; 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i); 6588c2ecf20Sopenharmony_ci data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i); 6598c2ecf20Sopenharmony_ci} 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_cistatic ssize_t update_interval_show(struct device *dev, 6628c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 6638c2ecf20Sopenharmony_ci{ 6648c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", data->update_interval); 6678c2ecf20Sopenharmony_ci} 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_cistatic ssize_t update_interval_store(struct device *dev, 6708c2ecf20Sopenharmony_ci struct device_attribute *attr, 6718c2ecf20Sopenharmony_ci const char *buf, size_t count) 6728c2ecf20Sopenharmony_ci{ 6738c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 6748c2ecf20Sopenharmony_ci unsigned long val; 6758c2ecf20Sopenharmony_ci int err; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 6788c2ecf20Sopenharmony_ci if (err) 6798c2ecf20Sopenharmony_ci return err; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6828c2ecf20Sopenharmony_ci lm63_set_convrate(data, clamp_val(val, 0, 100000)); 6838c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci return count; 6868c2ecf20Sopenharmony_ci} 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_cistatic ssize_t temp2_type_show(struct device *dev, 6898c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 6908c2ecf20Sopenharmony_ci{ 6918c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci return sprintf(buf, data->trutherm ? "1\n" : "2\n"); 6948c2ecf20Sopenharmony_ci} 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_cistatic ssize_t temp2_type_store(struct device *dev, 6978c2ecf20Sopenharmony_ci struct device_attribute *attr, 6988c2ecf20Sopenharmony_ci const char *buf, size_t count) 6998c2ecf20Sopenharmony_ci{ 7008c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 7018c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 7028c2ecf20Sopenharmony_ci unsigned long val; 7038c2ecf20Sopenharmony_ci int ret; 7048c2ecf20Sopenharmony_ci u8 reg; 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 10, &val); 7078c2ecf20Sopenharmony_ci if (ret < 0) 7088c2ecf20Sopenharmony_ci return ret; 7098c2ecf20Sopenharmony_ci if (val != 1 && val != 2) 7108c2ecf20Sopenharmony_ci return -EINVAL; 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 7138c2ecf20Sopenharmony_ci data->trutherm = val == 1; 7148c2ecf20Sopenharmony_ci reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02; 7158c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM, 7168c2ecf20Sopenharmony_ci reg | (data->trutherm ? 0x02 : 0x00)); 7178c2ecf20Sopenharmony_ci data->valid = 0; 7188c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci return count; 7218c2ecf20Sopenharmony_ci} 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *dummy, 7248c2ecf20Sopenharmony_ci char *buf) 7258c2ecf20Sopenharmony_ci{ 7268c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 7278c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", data->alarms); 7288c2ecf20Sopenharmony_ci} 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_cistatic ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, 7318c2ecf20Sopenharmony_ci char *buf) 7328c2ecf20Sopenharmony_ci{ 7338c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 7348c2ecf20Sopenharmony_ci struct lm63_data *data = lm63_update_device(dev); 7358c2ecf20Sopenharmony_ci int bitnr = attr->index; 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); 7388c2ecf20Sopenharmony_ci} 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); 7418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, 7428c2ecf20Sopenharmony_ci set_fan, 1); 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0); 7458c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(pwm1_enable); 7468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO, 7478c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 1); 7488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO, 7498c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 3); 7508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO, 7518c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 3); 7528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO, 7538c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 2); 7548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO, 7558c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 4); 7568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO, 7578c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 4); 7588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO, 7598c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 3); 7608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO, 7618c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 5); 7628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO, 7638c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 5); 7648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO, 7658c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 4); 7668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO, 7678c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 6); 7688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO, 7698c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 6); 7708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO, 7718c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 5); 7728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO, 7738c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 7); 7748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO, 7758c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 7); 7768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO, 7778c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 6); 7788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO, 7798c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 8); 7808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO, 7818c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 8); 7828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO, 7838c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 7); 7848c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO, 7858c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 9); 7868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO, 7878c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 9); 7888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO, 7898c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 8); 7908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO, 7918c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 10); 7928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO, 7938c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 10); 7948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO, 7958c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 9); 7968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO, 7978c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 11); 7988c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO, 7998c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 11); 8008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO, 8018c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 10); 8028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO, 8038c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 12); 8048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO, 8058c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 12); 8068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO, 8078c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 11); 8088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO, 8098c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 13); 8108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO, 8118c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 13); 8128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO, 8138c2ecf20Sopenharmony_ci show_pwm1, set_pwm1, 12); 8148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO, 8158c2ecf20Sopenharmony_ci show_lut_temp, set_temp8, 14); 8168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO, 8178c2ecf20Sopenharmony_ci show_lut_temp_hyst, NULL, 14); 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0); 8208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8, 8218c2ecf20Sopenharmony_ci set_temp8, 1); 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); 8248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, 8258c2ecf20Sopenharmony_ci set_temp11, 1); 8268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, 8278c2ecf20Sopenharmony_ci set_temp11, 2); 8288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, 8298c2ecf20Sopenharmony_ci set_temp11, 3); 8308c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8, 8318c2ecf20Sopenharmony_ci set_temp8, 2); 8328c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(temp2_crit_hyst); 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(temp2_type); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci/* Individual alarm files */ 8378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); 8388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); 8398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); 8408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); 8418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); 8428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); 8438c2ecf20Sopenharmony_ci/* Raw alarm file for compatibility */ 8448c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms); 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(update_interval); 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_cistatic struct attribute *lm63_attributes[] = { 8498c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1.dev_attr.attr, 8508c2ecf20Sopenharmony_ci &dev_attr_pwm1_enable.attr, 8518c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 8528c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, 8538c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr, 8548c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 8558c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, 8568c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr, 8578c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, 8588c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, 8598c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr, 8608c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, 8618c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, 8628c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr, 8638c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr, 8648c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr, 8658c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr, 8668c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr, 8678c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr, 8688c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr, 8698c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr, 8708c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr, 8718c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr, 8728c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr, 8738c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr, 8748c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr, 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 8778c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_input.dev_attr.attr, 8788c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_min.dev_attr.attr, 8798c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max.dev_attr.attr, 8808c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_max.dev_attr.attr, 8818c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_offset.dev_attr.attr, 8828c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_crit.dev_attr.attr, 8838c2ecf20Sopenharmony_ci &dev_attr_temp2_crit_hyst.attr, 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 8868c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_fault.dev_attr.attr, 8878c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 8888c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 8898c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 8908c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 8918c2ecf20Sopenharmony_ci &dev_attr_update_interval.attr, 8928c2ecf20Sopenharmony_ci NULL 8938c2ecf20Sopenharmony_ci}; 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_cistatic struct attribute *lm63_attributes_temp2_type[] = { 8968c2ecf20Sopenharmony_ci &dev_attr_temp2_type.attr, 8978c2ecf20Sopenharmony_ci NULL 8988c2ecf20Sopenharmony_ci}; 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_cistatic const struct attribute_group lm63_group_temp2_type = { 9018c2ecf20Sopenharmony_ci .attrs = lm63_attributes_temp2_type, 9028c2ecf20Sopenharmony_ci}; 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_cistatic struct attribute *lm63_attributes_extra_lut[] = { 9058c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr, 9068c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr, 9078c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr, 9088c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr, 9098c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr, 9108c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr, 9118c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr, 9128c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr, 9138c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr, 9148c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr, 9158c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr, 9168c2ecf20Sopenharmony_ci &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr, 9178c2ecf20Sopenharmony_ci NULL 9188c2ecf20Sopenharmony_ci}; 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_cistatic const struct attribute_group lm63_group_extra_lut = { 9218c2ecf20Sopenharmony_ci .attrs = lm63_attributes_extra_lut, 9228c2ecf20Sopenharmony_ci}; 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci/* 9258c2ecf20Sopenharmony_ci * On LM63, temp2_crit can be set only once, which should be job 9268c2ecf20Sopenharmony_ci * of the bootloader. 9278c2ecf20Sopenharmony_ci * On LM64, temp2_crit can always be set. 9288c2ecf20Sopenharmony_ci * On LM96163, temp2_crit can be set if bit 1 of the configuration 9298c2ecf20Sopenharmony_ci * register is true. 9308c2ecf20Sopenharmony_ci */ 9318c2ecf20Sopenharmony_cistatic umode_t lm63_attribute_mode(struct kobject *kobj, 9328c2ecf20Sopenharmony_ci struct attribute *attr, int index) 9338c2ecf20Sopenharmony_ci{ 9348c2ecf20Sopenharmony_ci struct device *dev = container_of(kobj, struct device, kobj); 9358c2ecf20Sopenharmony_ci struct lm63_data *data = dev_get_drvdata(dev); 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr 9388c2ecf20Sopenharmony_ci && (data->kind == lm64 || 9398c2ecf20Sopenharmony_ci (data->kind == lm96163 && (data->config & 0x02)))) 9408c2ecf20Sopenharmony_ci return attr->mode | S_IWUSR; 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_ci return attr->mode; 9438c2ecf20Sopenharmony_ci} 9448c2ecf20Sopenharmony_ci 9458c2ecf20Sopenharmony_cistatic const struct attribute_group lm63_group = { 9468c2ecf20Sopenharmony_ci .is_visible = lm63_attribute_mode, 9478c2ecf20Sopenharmony_ci .attrs = lm63_attributes, 9488c2ecf20Sopenharmony_ci}; 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_cistatic struct attribute *lm63_attributes_fan1[] = { 9518c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_input.dev_attr.attr, 9528c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_min.dev_attr.attr, 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, 9558c2ecf20Sopenharmony_ci NULL 9568c2ecf20Sopenharmony_ci}; 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_cistatic const struct attribute_group lm63_group_fan1 = { 9598c2ecf20Sopenharmony_ci .attrs = lm63_attributes_fan1, 9608c2ecf20Sopenharmony_ci}; 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci/* 9638c2ecf20Sopenharmony_ci * Real code 9648c2ecf20Sopenharmony_ci */ 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */ 9678c2ecf20Sopenharmony_cistatic int lm63_detect(struct i2c_client *client, 9688c2ecf20Sopenharmony_ci struct i2c_board_info *info) 9698c2ecf20Sopenharmony_ci{ 9708c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 9718c2ecf20Sopenharmony_ci u8 man_id, chip_id, reg_config1, reg_config2; 9728c2ecf20Sopenharmony_ci u8 reg_alert_status, reg_alert_mask; 9738c2ecf20Sopenharmony_ci int address = client->addr; 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 9768c2ecf20Sopenharmony_ci return -ENODEV; 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID); 9798c2ecf20Sopenharmony_ci chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID); 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_ci reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); 9828c2ecf20Sopenharmony_ci reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2); 9838c2ecf20Sopenharmony_ci reg_alert_status = i2c_smbus_read_byte_data(client, 9848c2ecf20Sopenharmony_ci LM63_REG_ALERT_STATUS); 9858c2ecf20Sopenharmony_ci reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK); 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci if (man_id != 0x01 /* National Semiconductor */ 9888c2ecf20Sopenharmony_ci || (reg_config1 & 0x18) != 0x00 9898c2ecf20Sopenharmony_ci || (reg_config2 & 0xF8) != 0x00 9908c2ecf20Sopenharmony_ci || (reg_alert_status & 0x20) != 0x00 9918c2ecf20Sopenharmony_ci || (reg_alert_mask & 0xA4) != 0xA4) { 9928c2ecf20Sopenharmony_ci dev_dbg(&adapter->dev, 9938c2ecf20Sopenharmony_ci "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", 9948c2ecf20Sopenharmony_ci man_id, chip_id); 9958c2ecf20Sopenharmony_ci return -ENODEV; 9968c2ecf20Sopenharmony_ci } 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_ci if (chip_id == 0x41 && address == 0x4c) 9998c2ecf20Sopenharmony_ci strlcpy(info->type, "lm63", I2C_NAME_SIZE); 10008c2ecf20Sopenharmony_ci else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e)) 10018c2ecf20Sopenharmony_ci strlcpy(info->type, "lm64", I2C_NAME_SIZE); 10028c2ecf20Sopenharmony_ci else if (chip_id == 0x49 && address == 0x4c) 10038c2ecf20Sopenharmony_ci strlcpy(info->type, "lm96163", I2C_NAME_SIZE); 10048c2ecf20Sopenharmony_ci else 10058c2ecf20Sopenharmony_ci return -ENODEV; 10068c2ecf20Sopenharmony_ci 10078c2ecf20Sopenharmony_ci return 0; 10088c2ecf20Sopenharmony_ci} 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_ci/* 10118c2ecf20Sopenharmony_ci * Ideally we shouldn't have to initialize anything, since the BIOS 10128c2ecf20Sopenharmony_ci * should have taken care of everything 10138c2ecf20Sopenharmony_ci */ 10148c2ecf20Sopenharmony_cistatic void lm63_init_client(struct lm63_data *data) 10158c2ecf20Sopenharmony_ci{ 10168c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 10178c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 10188c2ecf20Sopenharmony_ci u8 convrate; 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); 10218c2ecf20Sopenharmony_ci data->config_fan = i2c_smbus_read_byte_data(client, 10228c2ecf20Sopenharmony_ci LM63_REG_CONFIG_FAN); 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_ci /* Start converting if needed */ 10258c2ecf20Sopenharmony_ci if (data->config & 0x40) { /* standby */ 10268c2ecf20Sopenharmony_ci dev_dbg(dev, "Switching to operational mode\n"); 10278c2ecf20Sopenharmony_ci data->config &= 0xA7; 10288c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, 10298c2ecf20Sopenharmony_ci data->config); 10308c2ecf20Sopenharmony_ci } 10318c2ecf20Sopenharmony_ci /* Tachometer is always enabled on LM64 */ 10328c2ecf20Sopenharmony_ci if (data->kind == lm64) 10338c2ecf20Sopenharmony_ci data->config |= 0x04; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci /* We may need pwm1_freq before ever updating the client data */ 10368c2ecf20Sopenharmony_ci data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); 10378c2ecf20Sopenharmony_ci if (data->pwm1_freq == 0) 10388c2ecf20Sopenharmony_ci data->pwm1_freq = 1; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ci switch (data->kind) { 10418c2ecf20Sopenharmony_ci case lm63: 10428c2ecf20Sopenharmony_ci case lm64: 10438c2ecf20Sopenharmony_ci data->max_convrate_hz = LM63_MAX_CONVRATE_HZ; 10448c2ecf20Sopenharmony_ci data->lut_size = 8; 10458c2ecf20Sopenharmony_ci break; 10468c2ecf20Sopenharmony_ci case lm96163: 10478c2ecf20Sopenharmony_ci data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ; 10488c2ecf20Sopenharmony_ci data->lut_size = 12; 10498c2ecf20Sopenharmony_ci data->trutherm 10508c2ecf20Sopenharmony_ci = i2c_smbus_read_byte_data(client, 10518c2ecf20Sopenharmony_ci LM96163_REG_TRUTHERM) & 0x02; 10528c2ecf20Sopenharmony_ci break; 10538c2ecf20Sopenharmony_ci } 10548c2ecf20Sopenharmony_ci convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE); 10558c2ecf20Sopenharmony_ci if (unlikely(convrate > LM63_MAX_CONVRATE)) 10568c2ecf20Sopenharmony_ci convrate = LM63_MAX_CONVRATE; 10578c2ecf20Sopenharmony_ci data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, 10588c2ecf20Sopenharmony_ci convrate); 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ci /* 10618c2ecf20Sopenharmony_ci * For LM96163, check if high resolution PWM 10628c2ecf20Sopenharmony_ci * and unsigned temperature format is enabled. 10638c2ecf20Sopenharmony_ci */ 10648c2ecf20Sopenharmony_ci if (data->kind == lm96163) { 10658c2ecf20Sopenharmony_ci u8 config_enhanced 10668c2ecf20Sopenharmony_ci = i2c_smbus_read_byte_data(client, 10678c2ecf20Sopenharmony_ci LM96163_REG_CONFIG_ENHANCED); 10688c2ecf20Sopenharmony_ci if (config_enhanced & 0x20) 10698c2ecf20Sopenharmony_ci data->lut_temp_highres = true; 10708c2ecf20Sopenharmony_ci if ((config_enhanced & 0x10) 10718c2ecf20Sopenharmony_ci && !(data->config_fan & 0x08) && data->pwm1_freq == 8) 10728c2ecf20Sopenharmony_ci data->pwm_highres = true; 10738c2ecf20Sopenharmony_ci if (config_enhanced & 0x08) 10748c2ecf20Sopenharmony_ci data->remote_unsigned = true; 10758c2ecf20Sopenharmony_ci } 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci /* Show some debug info about the LM63 configuration */ 10788c2ecf20Sopenharmony_ci if (data->kind == lm63) 10798c2ecf20Sopenharmony_ci dev_dbg(dev, "Alert/tach pin configured for %s\n", 10808c2ecf20Sopenharmony_ci (data->config & 0x04) ? "tachometer input" : 10818c2ecf20Sopenharmony_ci "alert output"); 10828c2ecf20Sopenharmony_ci dev_dbg(dev, "PWM clock %s kHz, output frequency %u Hz\n", 10838c2ecf20Sopenharmony_ci (data->config_fan & 0x08) ? "1.4" : "360", 10848c2ecf20Sopenharmony_ci ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); 10858c2ecf20Sopenharmony_ci dev_dbg(dev, "PWM output active %s, %s mode\n", 10868c2ecf20Sopenharmony_ci (data->config_fan & 0x10) ? "low" : "high", 10878c2ecf20Sopenharmony_ci (data->config_fan & 0x20) ? "manual" : "auto"); 10888c2ecf20Sopenharmony_ci} 10898c2ecf20Sopenharmony_ci 10908c2ecf20Sopenharmony_cistatic const struct i2c_device_id lm63_id[]; 10918c2ecf20Sopenharmony_ci 10928c2ecf20Sopenharmony_cistatic int lm63_probe(struct i2c_client *client) 10938c2ecf20Sopenharmony_ci{ 10948c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 10958c2ecf20Sopenharmony_ci struct device *hwmon_dev; 10968c2ecf20Sopenharmony_ci struct lm63_data *data; 10978c2ecf20Sopenharmony_ci int groups = 0; 10988c2ecf20Sopenharmony_ci 10998c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL); 11008c2ecf20Sopenharmony_ci if (!data) 11018c2ecf20Sopenharmony_ci return -ENOMEM; 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci data->client = client; 11048c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_ci /* Set the device type */ 11078c2ecf20Sopenharmony_ci if (client->dev.of_node) 11088c2ecf20Sopenharmony_ci data->kind = (enum chips)of_device_get_match_data(&client->dev); 11098c2ecf20Sopenharmony_ci else 11108c2ecf20Sopenharmony_ci data->kind = i2c_match_id(lm63_id, client)->driver_data; 11118c2ecf20Sopenharmony_ci if (data->kind == lm64) 11128c2ecf20Sopenharmony_ci data->temp2_offset = 16000; 11138c2ecf20Sopenharmony_ci 11148c2ecf20Sopenharmony_ci /* Initialize chip */ 11158c2ecf20Sopenharmony_ci lm63_init_client(data); 11168c2ecf20Sopenharmony_ci 11178c2ecf20Sopenharmony_ci /* Register sysfs hooks */ 11188c2ecf20Sopenharmony_ci data->groups[groups++] = &lm63_group; 11198c2ecf20Sopenharmony_ci if (data->config & 0x04) /* tachometer enabled */ 11208c2ecf20Sopenharmony_ci data->groups[groups++] = &lm63_group_fan1; 11218c2ecf20Sopenharmony_ci 11228c2ecf20Sopenharmony_ci if (data->kind == lm96163) { 11238c2ecf20Sopenharmony_ci data->groups[groups++] = &lm63_group_temp2_type; 11248c2ecf20Sopenharmony_ci data->groups[groups++] = &lm63_group_extra_lut; 11258c2ecf20Sopenharmony_ci } 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 11288c2ecf20Sopenharmony_ci data, data->groups); 11298c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 11308c2ecf20Sopenharmony_ci} 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci/* 11338c2ecf20Sopenharmony_ci * Driver data (common to all clients) 11348c2ecf20Sopenharmony_ci */ 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_cistatic const struct i2c_device_id lm63_id[] = { 11378c2ecf20Sopenharmony_ci { "lm63", lm63 }, 11388c2ecf20Sopenharmony_ci { "lm64", lm64 }, 11398c2ecf20Sopenharmony_ci { "lm96163", lm96163 }, 11408c2ecf20Sopenharmony_ci { } 11418c2ecf20Sopenharmony_ci}; 11428c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, lm63_id); 11438c2ecf20Sopenharmony_ci 11448c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused lm63_of_match[] = { 11458c2ecf20Sopenharmony_ci { 11468c2ecf20Sopenharmony_ci .compatible = "national,lm63", 11478c2ecf20Sopenharmony_ci .data = (void *)lm63 11488c2ecf20Sopenharmony_ci }, 11498c2ecf20Sopenharmony_ci { 11508c2ecf20Sopenharmony_ci .compatible = "national,lm64", 11518c2ecf20Sopenharmony_ci .data = (void *)lm64 11528c2ecf20Sopenharmony_ci }, 11538c2ecf20Sopenharmony_ci { 11548c2ecf20Sopenharmony_ci .compatible = "national,lm96163", 11558c2ecf20Sopenharmony_ci .data = (void *)lm96163 11568c2ecf20Sopenharmony_ci }, 11578c2ecf20Sopenharmony_ci { }, 11588c2ecf20Sopenharmony_ci}; 11598c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lm63_of_match); 11608c2ecf20Sopenharmony_ci 11618c2ecf20Sopenharmony_cistatic struct i2c_driver lm63_driver = { 11628c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 11638c2ecf20Sopenharmony_ci .driver = { 11648c2ecf20Sopenharmony_ci .name = "lm63", 11658c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(lm63_of_match), 11668c2ecf20Sopenharmony_ci }, 11678c2ecf20Sopenharmony_ci .probe_new = lm63_probe, 11688c2ecf20Sopenharmony_ci .id_table = lm63_id, 11698c2ecf20Sopenharmony_ci .detect = lm63_detect, 11708c2ecf20Sopenharmony_ci .address_list = normal_i2c, 11718c2ecf20Sopenharmony_ci}; 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_cimodule_i2c_driver(lm63_driver); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); 11768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LM63 driver"); 11778c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1178