18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * adm9240.c Part of lm_sensors, Linux kernel modules for hardware 48c2ecf20Sopenharmony_ci * monitoring 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl> 78c2ecf20Sopenharmony_ci * Philip Edelbrock <phil@netroedge.com> 88c2ecf20Sopenharmony_ci * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl> 98c2ecf20Sopenharmony_ci * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable 108c2ecf20Sopenharmony_ci * guidance from Jean Delvare 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Driver supports Analog Devices ADM9240 138c2ecf20Sopenharmony_ci * Dallas Semiconductor DS1780 148c2ecf20Sopenharmony_ci * National Semiconductor LM81 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * ADM9240 is the reference, DS1780 and LM81 are register compatibles 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * Voltage Six inputs are scaled by chip, VID also reported 198c2ecf20Sopenharmony_ci * Temperature Chip temperature to 0.5'C, maximum and max_hysteris 208c2ecf20Sopenharmony_ci * Fans 2 fans, low speed alarm, automatic fan clock divider 218c2ecf20Sopenharmony_ci * Alarms 16-bit map of active alarms 228c2ecf20Sopenharmony_ci * Analog Out 0..1250 mV output 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm' 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * Test hardware: Intel SE440BX-2 desktop motherboard --Grant 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * LM81 extended temp reading not implemented 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include <linux/init.h> 328c2ecf20Sopenharmony_ci#include <linux/module.h> 338c2ecf20Sopenharmony_ci#include <linux/slab.h> 348c2ecf20Sopenharmony_ci#include <linux/i2c.h> 358c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 368c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 378c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h> 388c2ecf20Sopenharmony_ci#include <linux/err.h> 398c2ecf20Sopenharmony_ci#include <linux/mutex.h> 408c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 418c2ecf20Sopenharmony_ci#include <linux/regmap.h> 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* Addresses to scan */ 448c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, 458c2ecf20Sopenharmony_ci I2C_CLIENT_END }; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cienum chips { adm9240, ds1780, lm81 }; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/* ADM9240 registers */ 508c2ecf20Sopenharmony_ci#define ADM9240_REG_MAN_ID 0x3e 518c2ecf20Sopenharmony_ci#define ADM9240_REG_DIE_REV 0x3f 528c2ecf20Sopenharmony_ci#define ADM9240_REG_CONFIG 0x40 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */ 558c2ecf20Sopenharmony_ci#define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2) 568c2ecf20Sopenharmony_ci#define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2) 578c2ecf20Sopenharmony_ci#define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */ 588c2ecf20Sopenharmony_ci#define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr)) 598c2ecf20Sopenharmony_ci#define ADM9240_REG_INT(nr) (0x41 + (nr)) 608c2ecf20Sopenharmony_ci#define ADM9240_REG_INT_MASK(nr) (0x43 + (nr)) 618c2ecf20Sopenharmony_ci#define ADM9240_REG_TEMP 0x27 628c2ecf20Sopenharmony_ci#define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */ 638c2ecf20Sopenharmony_ci#define ADM9240_REG_ANALOG_OUT 0x19 648c2ecf20Sopenharmony_ci#define ADM9240_REG_CHASSIS_CLEAR 0x46 658c2ecf20Sopenharmony_ci#define ADM9240_REG_VID_FAN_DIV 0x47 668c2ecf20Sopenharmony_ci#define ADM9240_REG_I2C_ADDR 0x48 678c2ecf20Sopenharmony_ci#define ADM9240_REG_VID4 0x49 688c2ecf20Sopenharmony_ci#define ADM9240_REG_TEMP_CONF 0x4b 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/* generalised scaling with integer rounding */ 718c2ecf20Sopenharmony_cistatic inline int SCALE(long val, int mul, int div) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci if (val < 0) 748c2ecf20Sopenharmony_ci return (val * mul - div / 2) / div; 758c2ecf20Sopenharmony_ci else 768c2ecf20Sopenharmony_ci return (val * mul + div / 2) / div; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/* adm9240 internally scales voltage measurements */ 808c2ecf20Sopenharmony_cistatic const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 }; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic inline unsigned int IN_FROM_REG(u8 reg, int n) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci return SCALE(reg, nom_mv[n], 192); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic inline u8 IN_TO_REG(unsigned long val, int n) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci val = clamp_val(val, 0, nom_mv[n] * 255 / 192); 908c2ecf20Sopenharmony_ci return SCALE(val, 192, nom_mv[n]); 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci/* temperature range: -40..125, 127 disables temperature alarm */ 948c2ecf20Sopenharmony_cistatic inline s8 TEMP_TO_REG(long val) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci val = clamp_val(val, -40000, 127000); 978c2ecf20Sopenharmony_ci return SCALE(val, 1, 1000); 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* two fans, each with low fan speed limit */ 1018c2ecf20Sopenharmony_cistatic inline unsigned int FAN_FROM_REG(u8 reg, u8 div) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci if (!reg) /* error */ 1048c2ecf20Sopenharmony_ci return -1; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci if (reg == 255) 1078c2ecf20Sopenharmony_ci return 0; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci return SCALE(1350000, 1, reg * div); 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/* analog out 0..1250mV */ 1138c2ecf20Sopenharmony_cistatic inline u8 AOUT_TO_REG(unsigned long val) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci val = clamp_val(val, 0, 1250); 1168c2ecf20Sopenharmony_ci return SCALE(val, 255, 1250); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic inline unsigned int AOUT_FROM_REG(u8 reg) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci return SCALE(reg, 1250, 255); 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* per client data */ 1258c2ecf20Sopenharmony_cistruct adm9240_data { 1268c2ecf20Sopenharmony_ci struct i2c_client *client; 1278c2ecf20Sopenharmony_ci struct regmap *regmap; 1288c2ecf20Sopenharmony_ci struct mutex update_lock; 1298c2ecf20Sopenharmony_ci char valid; 1308c2ecf20Sopenharmony_ci unsigned long last_updated_measure; 1318c2ecf20Sopenharmony_ci unsigned long last_updated_config; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci u8 in[6]; /* ro in0_input */ 1348c2ecf20Sopenharmony_ci u8 in_max[6]; /* rw in0_max */ 1358c2ecf20Sopenharmony_ci u8 in_min[6]; /* rw in0_min */ 1368c2ecf20Sopenharmony_ci u8 fan[2]; /* ro fan1_input */ 1378c2ecf20Sopenharmony_ci u8 fan_min[2]; /* rw fan1_min */ 1388c2ecf20Sopenharmony_ci u8 fan_div[2]; /* rw fan1_div, read-only accessor */ 1398c2ecf20Sopenharmony_ci s16 temp; /* ro temp1_input, 9-bit sign-extended */ 1408c2ecf20Sopenharmony_ci s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */ 1418c2ecf20Sopenharmony_ci u16 alarms; /* ro alarms */ 1428c2ecf20Sopenharmony_ci u8 aout; /* rw aout_output */ 1438c2ecf20Sopenharmony_ci u8 vid; /* ro vid */ 1448c2ecf20Sopenharmony_ci u8 vrm; /* -- vrm set on startup, no accessor */ 1458c2ecf20Sopenharmony_ci}; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci/* write new fan div, callers must hold data->update_lock */ 1488c2ecf20Sopenharmony_cistatic int adm9240_write_fan_div(struct adm9240_data *data, int nr, 1498c2ecf20Sopenharmony_ci u8 fan_div) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci unsigned int reg, old, shift = (nr + 2) * 2; 1528c2ecf20Sopenharmony_ci int err; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, ®); 1558c2ecf20Sopenharmony_ci if (err < 0) 1568c2ecf20Sopenharmony_ci return err; 1578c2ecf20Sopenharmony_ci old = (reg >> shift) & 3; 1588c2ecf20Sopenharmony_ci reg &= ~(3 << shift); 1598c2ecf20Sopenharmony_ci reg |= (fan_div << shift); 1608c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_VID_FAN_DIV, reg); 1618c2ecf20Sopenharmony_ci if (err < 0) 1628c2ecf20Sopenharmony_ci return err; 1638c2ecf20Sopenharmony_ci dev_dbg(&data->client->dev, 1648c2ecf20Sopenharmony_ci "fan%d clock divider changed from %u to %u\n", 1658c2ecf20Sopenharmony_ci nr + 1, 1 << old, 1 << fan_div); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci return 0; 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic int adm9240_update_measure(struct adm9240_data *data) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci unsigned int val; 1738c2ecf20Sopenharmony_ci u8 regs[2]; 1748c2ecf20Sopenharmony_ci int err; 1758c2ecf20Sopenharmony_ci int i; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci err = regmap_bulk_read(data->regmap, ADM9240_REG_IN(0), &data->in[0], 6); 1788c2ecf20Sopenharmony_ci if (err < 0) 1798c2ecf20Sopenharmony_ci return err; 1808c2ecf20Sopenharmony_ci err = regmap_bulk_read(data->regmap, ADM9240_REG_INT(0), ®s, 2); 1818c2ecf20Sopenharmony_ci if (err < 0) 1828c2ecf20Sopenharmony_ci return err; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci data->alarms = regs[0] | regs[1] << 8; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci /* 1878c2ecf20Sopenharmony_ci * read temperature: assume temperature changes less than 1888c2ecf20Sopenharmony_ci * 0.5'C per two measurement cycles thus ignore possible 1898c2ecf20Sopenharmony_ci * but unlikely aliasing error on lsb reading. --Grant 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_ci err = regmap_read(data->regmap, ADM9240_REG_TEMP, &val); 1928c2ecf20Sopenharmony_ci if (err < 0) 1938c2ecf20Sopenharmony_ci return err; 1948c2ecf20Sopenharmony_ci data->temp = val << 8; 1958c2ecf20Sopenharmony_ci err = regmap_read(data->regmap, ADM9240_REG_TEMP_CONF, &val); 1968c2ecf20Sopenharmony_ci if (err < 0) 1978c2ecf20Sopenharmony_ci return err; 1988c2ecf20Sopenharmony_ci data->temp |= val; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci err = regmap_bulk_read(data->regmap, ADM9240_REG_FAN(0), 2018c2ecf20Sopenharmony_ci &data->fan[0], 2); 2028c2ecf20Sopenharmony_ci if (err < 0) 2038c2ecf20Sopenharmony_ci return err; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { /* read fans */ 2068c2ecf20Sopenharmony_ci /* adjust fan clock divider on overflow */ 2078c2ecf20Sopenharmony_ci if (data->valid && data->fan[i] == 255 && 2088c2ecf20Sopenharmony_ci data->fan_div[i] < 3) { 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci err = adm9240_write_fan_div(data, i, 2118c2ecf20Sopenharmony_ci ++data->fan_div[i]); 2128c2ecf20Sopenharmony_ci if (err < 0) 2138c2ecf20Sopenharmony_ci return err; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci /* adjust fan_min if active, but not to 0 */ 2168c2ecf20Sopenharmony_ci if (data->fan_min[i] < 255 && 2178c2ecf20Sopenharmony_ci data->fan_min[i] >= 2) 2188c2ecf20Sopenharmony_ci data->fan_min[i] /= 2; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci return 0; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_cistatic int adm9240_update_config(struct adm9240_data *data) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci unsigned int val; 2288c2ecf20Sopenharmony_ci int i; 2298c2ecf20Sopenharmony_ci int err; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci for (i = 0; i < 6; i++) { 2328c2ecf20Sopenharmony_ci err = regmap_raw_read(data->regmap, ADM9240_REG_IN_MIN(i), 2338c2ecf20Sopenharmony_ci &data->in_min[i], 1); 2348c2ecf20Sopenharmony_ci if (err < 0) 2358c2ecf20Sopenharmony_ci return err; 2368c2ecf20Sopenharmony_ci err = regmap_raw_read(data->regmap, ADM9240_REG_IN_MAX(i), 2378c2ecf20Sopenharmony_ci &data->in_max[i], 1); 2388c2ecf20Sopenharmony_ci if (err < 0) 2398c2ecf20Sopenharmony_ci return err; 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci err = regmap_bulk_read(data->regmap, ADM9240_REG_FAN_MIN(0), 2428c2ecf20Sopenharmony_ci &data->fan_min[0], 2); 2438c2ecf20Sopenharmony_ci if (err < 0) 2448c2ecf20Sopenharmony_ci return err; 2458c2ecf20Sopenharmony_ci err = regmap_bulk_read(data->regmap, ADM9240_REG_TEMP_MAX(0), 2468c2ecf20Sopenharmony_ci &data->temp_max[0], 2); 2478c2ecf20Sopenharmony_ci if (err < 0) 2488c2ecf20Sopenharmony_ci return err; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci /* read fan divs and 5-bit VID */ 2518c2ecf20Sopenharmony_ci err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, &val); 2528c2ecf20Sopenharmony_ci if (err < 0) 2538c2ecf20Sopenharmony_ci return err; 2548c2ecf20Sopenharmony_ci data->fan_div[0] = (val >> 4) & 3; 2558c2ecf20Sopenharmony_ci data->fan_div[1] = (val >> 6) & 3; 2568c2ecf20Sopenharmony_ci data->vid = val & 0x0f; 2578c2ecf20Sopenharmony_ci err = regmap_read(data->regmap, ADM9240_REG_VID4, &val); 2588c2ecf20Sopenharmony_ci if (err < 0) 2598c2ecf20Sopenharmony_ci return err; 2608c2ecf20Sopenharmony_ci data->vid |= (val & 1) << 4; 2618c2ecf20Sopenharmony_ci /* read analog out */ 2628c2ecf20Sopenharmony_ci err = regmap_raw_read(data->regmap, ADM9240_REG_ANALOG_OUT, 2638c2ecf20Sopenharmony_ci &data->aout, 1); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci return err; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic struct adm9240_data *adm9240_update_device(struct device *dev) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 2718c2ecf20Sopenharmony_ci int err; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci /* minimum measurement cycle: 1.75 seconds */ 2768c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) 2778c2ecf20Sopenharmony_ci || !data->valid) { 2788c2ecf20Sopenharmony_ci err = adm9240_update_measure(data); 2798c2ecf20Sopenharmony_ci if (err < 0) { 2808c2ecf20Sopenharmony_ci data->valid = 0; 2818c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 2828c2ecf20Sopenharmony_ci return ERR_PTR(err); 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci data->last_updated_measure = jiffies; 2858c2ecf20Sopenharmony_ci } 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci /* minimum config reading cycle: 300 seconds */ 2888c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated_config + (HZ * 300)) 2898c2ecf20Sopenharmony_ci || !data->valid) { 2908c2ecf20Sopenharmony_ci err = adm9240_update_config(data); 2918c2ecf20Sopenharmony_ci if (err < 0) { 2928c2ecf20Sopenharmony_ci data->valid = 0; 2938c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 2948c2ecf20Sopenharmony_ci return ERR_PTR(err); 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci data->last_updated_config = jiffies; 2978c2ecf20Sopenharmony_ci data->valid = 1; 2988c2ecf20Sopenharmony_ci } 2998c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 3008c2ecf20Sopenharmony_ci return data; 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci/*** sysfs accessors ***/ 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci/* temperature */ 3068c2ecf20Sopenharmony_cistatic ssize_t temp1_input_show(struct device *dev, 3078c2ecf20Sopenharmony_ci struct device_attribute *dummy, char *buf) 3088c2ecf20Sopenharmony_ci{ 3098c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3128c2ecf20Sopenharmony_ci return PTR_ERR(data); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */ 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_cistatic ssize_t max_show(struct device *dev, struct device_attribute *devattr, 3188c2ecf20Sopenharmony_ci char *buf) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3218c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3248c2ecf20Sopenharmony_ci return PTR_ERR(data); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); 3278c2ecf20Sopenharmony_ci} 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_cistatic ssize_t max_store(struct device *dev, struct device_attribute *devattr, 3308c2ecf20Sopenharmony_ci const char *buf, size_t count) 3318c2ecf20Sopenharmony_ci{ 3328c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3338c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 3348c2ecf20Sopenharmony_ci long val; 3358c2ecf20Sopenharmony_ci int err; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 3388c2ecf20Sopenharmony_ci if (err) 3398c2ecf20Sopenharmony_ci return err; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 3428c2ecf20Sopenharmony_ci data->temp_max[attr->index] = TEMP_TO_REG(val); 3438c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_TEMP_MAX(attr->index), 3448c2ecf20Sopenharmony_ci data->temp_max[attr->index]); 3458c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 3468c2ecf20Sopenharmony_ci return err < 0 ? err : count; 3478c2ecf20Sopenharmony_ci} 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(temp1_input); 3508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0); 3518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, max, 1); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/* voltage */ 3548c2ecf20Sopenharmony_cistatic ssize_t in_show(struct device *dev, struct device_attribute *devattr, 3558c2ecf20Sopenharmony_ci char *buf) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3588c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3618c2ecf20Sopenharmony_ci return PTR_ERR(data); 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], 3648c2ecf20Sopenharmony_ci attr->index)); 3658c2ecf20Sopenharmony_ci} 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_cistatic ssize_t in_min_show(struct device *dev, 3688c2ecf20Sopenharmony_ci struct device_attribute *devattr, char *buf) 3698c2ecf20Sopenharmony_ci{ 3708c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3718c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3748c2ecf20Sopenharmony_ci return PTR_ERR(data); 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], 3778c2ecf20Sopenharmony_ci attr->index)); 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic ssize_t in_max_show(struct device *dev, 3818c2ecf20Sopenharmony_ci struct device_attribute *devattr, char *buf) 3828c2ecf20Sopenharmony_ci{ 3838c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3848c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3878c2ecf20Sopenharmony_ci return PTR_ERR(data); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], 3908c2ecf20Sopenharmony_ci attr->index)); 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, 3948c2ecf20Sopenharmony_ci struct device_attribute *devattr, const char *buf, 3958c2ecf20Sopenharmony_ci size_t count) 3968c2ecf20Sopenharmony_ci{ 3978c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 3988c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 3998c2ecf20Sopenharmony_ci unsigned long val; 4008c2ecf20Sopenharmony_ci int err; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 4038c2ecf20Sopenharmony_ci if (err) 4048c2ecf20Sopenharmony_ci return err; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4078c2ecf20Sopenharmony_ci data->in_min[attr->index] = IN_TO_REG(val, attr->index); 4088c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_IN_MIN(attr->index), 4098c2ecf20Sopenharmony_ci data->in_min[attr->index]); 4108c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4118c2ecf20Sopenharmony_ci return err < 0 ? err : count; 4128c2ecf20Sopenharmony_ci} 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, 4158c2ecf20Sopenharmony_ci struct device_attribute *devattr, const char *buf, 4168c2ecf20Sopenharmony_ci size_t count) 4178c2ecf20Sopenharmony_ci{ 4188c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4198c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 4208c2ecf20Sopenharmony_ci unsigned long val; 4218c2ecf20Sopenharmony_ci int err; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 4248c2ecf20Sopenharmony_ci if (err) 4258c2ecf20Sopenharmony_ci return err; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4288c2ecf20Sopenharmony_ci data->in_max[attr->index] = IN_TO_REG(val, attr->index); 4298c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_IN_MAX(attr->index), 4308c2ecf20Sopenharmony_ci data->in_max[attr->index]); 4318c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4328c2ecf20Sopenharmony_ci return err < 0 ? err : count; 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in, 0); 4368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0); 4378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0); 4388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in, 1); 4398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1); 4408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1); 4418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in, 2); 4428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2); 4438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2); 4448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in, 3); 4458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3); 4468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3); 4478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in, 4); 4488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4); 4498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4); 4508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_input, in, 5); 4518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5); 4528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5); 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci/* fans */ 4558c2ecf20Sopenharmony_cistatic ssize_t fan_show(struct device *dev, struct device_attribute *devattr, 4568c2ecf20Sopenharmony_ci char *buf) 4578c2ecf20Sopenharmony_ci{ 4588c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4598c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci if (IS_ERR(data)) 4628c2ecf20Sopenharmony_ci return PTR_ERR(data); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], 4658c2ecf20Sopenharmony_ci 1 << data->fan_div[attr->index])); 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, 4698c2ecf20Sopenharmony_ci struct device_attribute *devattr, char *buf) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4728c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci if (IS_ERR(data)) 4758c2ecf20Sopenharmony_ci return PTR_ERR(data); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], 4788c2ecf20Sopenharmony_ci 1 << data->fan_div[attr->index])); 4798c2ecf20Sopenharmony_ci} 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_cistatic ssize_t fan_div_show(struct device *dev, 4828c2ecf20Sopenharmony_ci struct device_attribute *devattr, char *buf) 4838c2ecf20Sopenharmony_ci{ 4848c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 4858c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci if (IS_ERR(data)) 4888c2ecf20Sopenharmony_ci return PTR_ERR(data); 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci/* 4948c2ecf20Sopenharmony_ci * set fan speed low limit: 4958c2ecf20Sopenharmony_ci * 4968c2ecf20Sopenharmony_ci * - value is zero: disable fan speed low limit alarm 4978c2ecf20Sopenharmony_ci * 4988c2ecf20Sopenharmony_ci * - value is below fan speed measurement range: enable fan speed low 4998c2ecf20Sopenharmony_ci * limit alarm to be asserted while fan speed too slow to measure 5008c2ecf20Sopenharmony_ci * 5018c2ecf20Sopenharmony_ci * - otherwise: select fan clock divider to suit fan speed low limit, 5028c2ecf20Sopenharmony_ci * measurement code may adjust registers to ensure fan speed reading 5038c2ecf20Sopenharmony_ci */ 5048c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev, 5058c2ecf20Sopenharmony_ci struct device_attribute *devattr, 5068c2ecf20Sopenharmony_ci const char *buf, size_t count) 5078c2ecf20Sopenharmony_ci{ 5088c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 5098c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 5108c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 5118c2ecf20Sopenharmony_ci int nr = attr->index; 5128c2ecf20Sopenharmony_ci u8 new_div; 5138c2ecf20Sopenharmony_ci unsigned long val; 5148c2ecf20Sopenharmony_ci int err; 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 5178c2ecf20Sopenharmony_ci if (err) 5188c2ecf20Sopenharmony_ci return err; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci if (!val) { 5238c2ecf20Sopenharmony_ci data->fan_min[nr] = 255; 5248c2ecf20Sopenharmony_ci new_div = data->fan_div[nr]; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "fan%u low limit set disabled\n", 5278c2ecf20Sopenharmony_ci nr + 1); 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci } else if (val < 1350000 / (8 * 254)) { 5308c2ecf20Sopenharmony_ci new_div = 3; 5318c2ecf20Sopenharmony_ci data->fan_min[nr] = 254; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", 5348c2ecf20Sopenharmony_ci nr + 1, FAN_FROM_REG(254, 1 << new_div)); 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci } else { 5378c2ecf20Sopenharmony_ci unsigned int new_min = 1350000 / val; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci new_div = 0; 5408c2ecf20Sopenharmony_ci while (new_min > 192 && new_div < 3) { 5418c2ecf20Sopenharmony_ci new_div++; 5428c2ecf20Sopenharmony_ci new_min /= 2; 5438c2ecf20Sopenharmony_ci } 5448c2ecf20Sopenharmony_ci if (!new_min) /* keep > 0 */ 5458c2ecf20Sopenharmony_ci new_min++; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci data->fan_min[nr] = new_min; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", 5508c2ecf20Sopenharmony_ci nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci if (new_div != data->fan_div[nr]) { 5548c2ecf20Sopenharmony_ci data->fan_div[nr] = new_div; 5558c2ecf20Sopenharmony_ci adm9240_write_fan_div(data, nr, new_div); 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_FAN_MIN(nr), 5588c2ecf20Sopenharmony_ci data->fan_min[nr]); 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 5618c2ecf20Sopenharmony_ci return err < 0 ? err : count; 5628c2ecf20Sopenharmony_ci} 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0); 5658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); 5668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_div, fan_div, 0); 5678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1); 5688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); 5698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_div, fan_div, 1); 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci/* alarms */ 5728c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, 5738c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 5748c2ecf20Sopenharmony_ci{ 5758c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci if (IS_ERR(data)) 5788c2ecf20Sopenharmony_ci return PTR_ERR(data); 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", data->alarms); 5818c2ecf20Sopenharmony_ci} 5828c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms); 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr, 5858c2ecf20Sopenharmony_ci char *buf) 5868c2ecf20Sopenharmony_ci{ 5878c2ecf20Sopenharmony_ci int bitnr = to_sensor_dev_attr(attr)->index; 5888c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci if (IS_ERR(data)) 5918c2ecf20Sopenharmony_ci return PTR_ERR(data); 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); 5948c2ecf20Sopenharmony_ci} 5958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); 5968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); 5978c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); 5988c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); 5998c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8); 6008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9); 6018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4); 6028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6); 6038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7); 6048c2ecf20Sopenharmony_ci 6058c2ecf20Sopenharmony_ci/* vid */ 6068c2ecf20Sopenharmony_cistatic ssize_t cpu0_vid_show(struct device *dev, 6078c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 6088c2ecf20Sopenharmony_ci{ 6098c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci if (IS_ERR(data)) 6128c2ecf20Sopenharmony_ci return PTR_ERR(data); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 6158c2ecf20Sopenharmony_ci} 6168c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid); 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci/* analog output */ 6198c2ecf20Sopenharmony_cistatic ssize_t aout_output_show(struct device *dev, 6208c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 6218c2ecf20Sopenharmony_ci{ 6228c2ecf20Sopenharmony_ci struct adm9240_data *data = adm9240_update_device(dev); 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_ci if (IS_ERR(data)) 6258c2ecf20Sopenharmony_ci return PTR_ERR(data); 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); 6288c2ecf20Sopenharmony_ci} 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_cistatic ssize_t aout_output_store(struct device *dev, 6318c2ecf20Sopenharmony_ci struct device_attribute *attr, 6328c2ecf20Sopenharmony_ci const char *buf, size_t count) 6338c2ecf20Sopenharmony_ci{ 6348c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 6358c2ecf20Sopenharmony_ci long val; 6368c2ecf20Sopenharmony_ci int err; 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 6398c2ecf20Sopenharmony_ci if (err) 6408c2ecf20Sopenharmony_ci return err; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6438c2ecf20Sopenharmony_ci data->aout = AOUT_TO_REG(val); 6448c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_ANALOG_OUT, data->aout); 6458c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6468c2ecf20Sopenharmony_ci return err < 0 ? err : count; 6478c2ecf20Sopenharmony_ci} 6488c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(aout_output); 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_cistatic ssize_t alarm_store(struct device *dev, struct device_attribute *attr, 6518c2ecf20Sopenharmony_ci const char *buf, size_t count) 6528c2ecf20Sopenharmony_ci{ 6538c2ecf20Sopenharmony_ci struct adm9240_data *data = dev_get_drvdata(dev); 6548c2ecf20Sopenharmony_ci unsigned long val; 6558c2ecf20Sopenharmony_ci int err; 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci if (kstrtoul(buf, 10, &val) || val != 0) 6588c2ecf20Sopenharmony_ci return -EINVAL; 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6618c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_CHASSIS_CLEAR, 0x80); 6628c2ecf20Sopenharmony_ci data->valid = 0; /* Force cache refresh */ 6638c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6648c2ecf20Sopenharmony_ci if (err < 0) 6658c2ecf20Sopenharmony_ci return err; 6668c2ecf20Sopenharmony_ci dev_dbg(&data->client->dev, "chassis intrusion latch cleared\n"); 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci return count; 6698c2ecf20Sopenharmony_ci} 6708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(intrusion0_alarm, alarm, 12); 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_cistatic struct attribute *adm9240_attrs[] = { 6738c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_input.dev_attr.attr, 6748c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_min.dev_attr.attr, 6758c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_max.dev_attr.attr, 6768c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_alarm.dev_attr.attr, 6778c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_input.dev_attr.attr, 6788c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_min.dev_attr.attr, 6798c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_max.dev_attr.attr, 6808c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_alarm.dev_attr.attr, 6818c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_input.dev_attr.attr, 6828c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_min.dev_attr.attr, 6838c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_max.dev_attr.attr, 6848c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_alarm.dev_attr.attr, 6858c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_input.dev_attr.attr, 6868c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_min.dev_attr.attr, 6878c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_max.dev_attr.attr, 6888c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_alarm.dev_attr.attr, 6898c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_input.dev_attr.attr, 6908c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_min.dev_attr.attr, 6918c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_max.dev_attr.attr, 6928c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_alarm.dev_attr.attr, 6938c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_input.dev_attr.attr, 6948c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_min.dev_attr.attr, 6958c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_max.dev_attr.attr, 6968c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_alarm.dev_attr.attr, 6978c2ecf20Sopenharmony_ci &dev_attr_temp1_input.attr, 6988c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max.dev_attr.attr, 6998c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 7008c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_alarm.dev_attr.attr, 7018c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_input.dev_attr.attr, 7028c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_div.dev_attr.attr, 7038c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_min.dev_attr.attr, 7048c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_alarm.dev_attr.attr, 7058c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_input.dev_attr.attr, 7068c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_div.dev_attr.attr, 7078c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_min.dev_attr.attr, 7088c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_alarm.dev_attr.attr, 7098c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 7108c2ecf20Sopenharmony_ci &dev_attr_aout_output.attr, 7118c2ecf20Sopenharmony_ci &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, 7128c2ecf20Sopenharmony_ci &dev_attr_cpu0_vid.attr, 7138c2ecf20Sopenharmony_ci NULL 7148c2ecf20Sopenharmony_ci}; 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(adm9240); 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci/*** sensor chip detect and driver install ***/ 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */ 7218c2ecf20Sopenharmony_cistatic int adm9240_detect(struct i2c_client *new_client, 7228c2ecf20Sopenharmony_ci struct i2c_board_info *info) 7238c2ecf20Sopenharmony_ci{ 7248c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = new_client->adapter; 7258c2ecf20Sopenharmony_ci const char *name = ""; 7268c2ecf20Sopenharmony_ci int address = new_client->addr; 7278c2ecf20Sopenharmony_ci u8 man_id, die_rev; 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 7308c2ecf20Sopenharmony_ci return -ENODEV; 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci /* verify chip: reg address should match i2c address */ 7338c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) 7348c2ecf20Sopenharmony_ci != address) { 7358c2ecf20Sopenharmony_ci dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n", 7368c2ecf20Sopenharmony_ci address); 7378c2ecf20Sopenharmony_ci return -ENODEV; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci /* check known chip manufacturer */ 7418c2ecf20Sopenharmony_ci man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID); 7428c2ecf20Sopenharmony_ci if (man_id == 0x23) { 7438c2ecf20Sopenharmony_ci name = "adm9240"; 7448c2ecf20Sopenharmony_ci } else if (man_id == 0xda) { 7458c2ecf20Sopenharmony_ci name = "ds1780"; 7468c2ecf20Sopenharmony_ci } else if (man_id == 0x01) { 7478c2ecf20Sopenharmony_ci name = "lm81"; 7488c2ecf20Sopenharmony_ci } else { 7498c2ecf20Sopenharmony_ci dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n", 7508c2ecf20Sopenharmony_ci man_id); 7518c2ecf20Sopenharmony_ci return -ENODEV; 7528c2ecf20Sopenharmony_ci } 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci /* successful detect, print chip info */ 7558c2ecf20Sopenharmony_ci die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV); 7568c2ecf20Sopenharmony_ci dev_info(&adapter->dev, "found %s revision %u\n", 7578c2ecf20Sopenharmony_ci man_id == 0x23 ? "ADM9240" : 7588c2ecf20Sopenharmony_ci man_id == 0xda ? "DS1780" : "LM81", die_rev); 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci strlcpy(info->type, name, I2C_NAME_SIZE); 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci return 0; 7638c2ecf20Sopenharmony_ci} 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_cistatic int adm9240_init_client(struct i2c_client *client, struct adm9240_data *data) 7668c2ecf20Sopenharmony_ci{ 7678c2ecf20Sopenharmony_ci u8 conf, mode; 7688c2ecf20Sopenharmony_ci int err; 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci err = regmap_raw_read(data->regmap, ADM9240_REG_CONFIG, &conf, 1); 7718c2ecf20Sopenharmony_ci if (err < 0) 7728c2ecf20Sopenharmony_ci return err; 7738c2ecf20Sopenharmony_ci err = regmap_raw_read(data->regmap, ADM9240_REG_TEMP_CONF, &mode, 1); 7748c2ecf20Sopenharmony_ci if (err < 0) 7758c2ecf20Sopenharmony_ci return err; 7768c2ecf20Sopenharmony_ci mode &= 3; 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci data->vrm = vid_which_vrm(); /* need this to report vid as mV */ 7798c2ecf20Sopenharmony_ci 7808c2ecf20Sopenharmony_ci dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, 7818c2ecf20Sopenharmony_ci data->vrm % 10); 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci if (conf & 1) { /* measurement cycle running: report state */ 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci dev_info(&client->dev, "status: config 0x%02x mode %u\n", 7868c2ecf20Sopenharmony_ci conf, mode); 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci } else { /* cold start: open limits before starting chip */ 7898c2ecf20Sopenharmony_ci int i; 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci for (i = 0; i < 6; i++) { 7928c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, 7938c2ecf20Sopenharmony_ci ADM9240_REG_IN_MIN(i), 0); 7948c2ecf20Sopenharmony_ci if (err < 0) 7958c2ecf20Sopenharmony_ci return err; 7968c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, 7978c2ecf20Sopenharmony_ci ADM9240_REG_IN_MAX(i), 255); 7988c2ecf20Sopenharmony_ci if (err < 0) 7998c2ecf20Sopenharmony_ci return err; 8008c2ecf20Sopenharmony_ci } 8018c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 8028c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, 8038c2ecf20Sopenharmony_ci ADM9240_REG_FAN_MIN(i), 255); 8048c2ecf20Sopenharmony_ci if (err < 0) 8058c2ecf20Sopenharmony_ci return err; 8068c2ecf20Sopenharmony_ci } 8078c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 8088c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, 8098c2ecf20Sopenharmony_ci ADM9240_REG_TEMP_MAX(i), 127); 8108c2ecf20Sopenharmony_ci if (err < 0) 8118c2ecf20Sopenharmony_ci return err; 8128c2ecf20Sopenharmony_ci } 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci /* start measurement cycle */ 8158c2ecf20Sopenharmony_ci err = regmap_write(data->regmap, ADM9240_REG_CONFIG, 1); 8168c2ecf20Sopenharmony_ci if (err < 0) 8178c2ecf20Sopenharmony_ci return err; 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci dev_info(&client->dev, 8208c2ecf20Sopenharmony_ci "cold start: config was 0x%02x mode %u\n", conf, mode); 8218c2ecf20Sopenharmony_ci } 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci return 0; 8248c2ecf20Sopenharmony_ci} 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_cistatic const struct regmap_config adm9240_regmap_config = { 8278c2ecf20Sopenharmony_ci .reg_bits = 8, 8288c2ecf20Sopenharmony_ci .val_bits = 8, 8298c2ecf20Sopenharmony_ci .use_single_read = true, 8308c2ecf20Sopenharmony_ci .use_single_write = true, 8318c2ecf20Sopenharmony_ci}; 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_cistatic int adm9240_probe(struct i2c_client *new_client) 8348c2ecf20Sopenharmony_ci{ 8358c2ecf20Sopenharmony_ci struct device *dev = &new_client->dev; 8368c2ecf20Sopenharmony_ci struct device *hwmon_dev; 8378c2ecf20Sopenharmony_ci struct adm9240_data *data; 8388c2ecf20Sopenharmony_ci int err; 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 8418c2ecf20Sopenharmony_ci if (!data) 8428c2ecf20Sopenharmony_ci return -ENOMEM; 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci data->client = new_client; 8458c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 8468c2ecf20Sopenharmony_ci data->regmap = devm_regmap_init_i2c(new_client, &adm9240_regmap_config); 8478c2ecf20Sopenharmony_ci if (IS_ERR(data->regmap)) 8488c2ecf20Sopenharmony_ci return PTR_ERR(data->regmap); 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci err = adm9240_init_client(new_client, data); 8518c2ecf20Sopenharmony_ci if (err < 0) 8528c2ecf20Sopenharmony_ci return err; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, 8558c2ecf20Sopenharmony_ci new_client->name, 8568c2ecf20Sopenharmony_ci data, 8578c2ecf20Sopenharmony_ci adm9240_groups); 8588c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 8598c2ecf20Sopenharmony_ci} 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_cistatic const struct i2c_device_id adm9240_id[] = { 8628c2ecf20Sopenharmony_ci { "adm9240", adm9240 }, 8638c2ecf20Sopenharmony_ci { "ds1780", ds1780 }, 8648c2ecf20Sopenharmony_ci { "lm81", lm81 }, 8658c2ecf20Sopenharmony_ci { } 8668c2ecf20Sopenharmony_ci}; 8678c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, adm9240_id); 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_cistatic struct i2c_driver adm9240_driver = { 8708c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 8718c2ecf20Sopenharmony_ci .driver = { 8728c2ecf20Sopenharmony_ci .name = "adm9240", 8738c2ecf20Sopenharmony_ci }, 8748c2ecf20Sopenharmony_ci .probe_new = adm9240_probe, 8758c2ecf20Sopenharmony_ci .id_table = adm9240_id, 8768c2ecf20Sopenharmony_ci .detect = adm9240_detect, 8778c2ecf20Sopenharmony_ci .address_list = normal_i2c, 8788c2ecf20Sopenharmony_ci}; 8798c2ecf20Sopenharmony_ci 8808c2ecf20Sopenharmony_cimodule_i2c_driver(adm9240_driver); 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " 8838c2ecf20Sopenharmony_ci "Grant Coady <gcoady.lk@gmail.com> and others"); 8848c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); 8858c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 886