18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware 48c2ecf20Sopenharmony_ci * monitoring 58c2ecf20Sopenharmony_ci * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>, 68c2ecf20Sopenharmony_ci * Kyösti Mälkki <kmalkki@cc.hut.fi> 78c2ecf20Sopenharmony_ci * Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/init.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 148c2ecf20Sopenharmony_ci#include <linux/i2c.h> 158c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 168c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 178c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h> 188c2ecf20Sopenharmony_ci#include <linux/err.h> 198c2ecf20Sopenharmony_ci#include <linux/mutex.h> 208c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* Type of the extra sensor */ 238c2ecf20Sopenharmony_cistatic unsigned short extra_sensor_type; 248c2ecf20Sopenharmony_cimodule_param(extra_sensor_type, ushort, 0); 258c2ecf20Sopenharmony_ciMODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)"); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* Addresses to scan */ 288c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* 318c2ecf20Sopenharmony_ci * Many GL520 constants specified below 328c2ecf20Sopenharmony_ci * One of the inputs can be configured as either temp or voltage. 338c2ecf20Sopenharmony_ci * That's why _TEMP2 and _IN4 access the same register 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* The GL520 registers */ 378c2ecf20Sopenharmony_ci#define GL520_REG_CHIP_ID 0x00 388c2ecf20Sopenharmony_ci#define GL520_REG_REVISION 0x01 398c2ecf20Sopenharmony_ci#define GL520_REG_CONF 0x03 408c2ecf20Sopenharmony_ci#define GL520_REG_MASK 0x11 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define GL520_REG_VID_INPUT 0x02 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e }; 458c2ecf20Sopenharmony_cistatic const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b }; 468c2ecf20Sopenharmony_cistatic const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 }; 478c2ecf20Sopenharmony_cistatic const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 }; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e }; 508c2ecf20Sopenharmony_cistatic const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 }; 518c2ecf20Sopenharmony_cistatic const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 }; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define GL520_REG_FAN_INPUT 0x07 548c2ecf20Sopenharmony_ci#define GL520_REG_FAN_MIN 0x08 558c2ecf20Sopenharmony_ci#define GL520_REG_FAN_DIV 0x0f 568c2ecf20Sopenharmony_ci#define GL520_REG_FAN_OFF GL520_REG_FAN_DIV 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define GL520_REG_ALARMS 0x12 598c2ecf20Sopenharmony_ci#define GL520_REG_BEEP_MASK 0x10 608c2ecf20Sopenharmony_ci#define GL520_REG_BEEP_ENABLE GL520_REG_CONF 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* Client data */ 638c2ecf20Sopenharmony_cistruct gl520_data { 648c2ecf20Sopenharmony_ci struct i2c_client *client; 658c2ecf20Sopenharmony_ci const struct attribute_group *groups[3]; 668c2ecf20Sopenharmony_ci struct mutex update_lock; 678c2ecf20Sopenharmony_ci char valid; /* zero until the following fields are valid */ 688c2ecf20Sopenharmony_ci unsigned long last_updated; /* in jiffies */ 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci u8 vid; 718c2ecf20Sopenharmony_ci u8 vrm; 728c2ecf20Sopenharmony_ci u8 in_input[5]; /* [0] = VVD */ 738c2ecf20Sopenharmony_ci u8 in_min[5]; /* [0] = VDD */ 748c2ecf20Sopenharmony_ci u8 in_max[5]; /* [0] = VDD */ 758c2ecf20Sopenharmony_ci u8 fan_input[2]; 768c2ecf20Sopenharmony_ci u8 fan_min[2]; 778c2ecf20Sopenharmony_ci u8 fan_div[2]; 788c2ecf20Sopenharmony_ci u8 fan_off; 798c2ecf20Sopenharmony_ci u8 temp_input[2]; 808c2ecf20Sopenharmony_ci u8 temp_max[2]; 818c2ecf20Sopenharmony_ci u8 temp_max_hyst[2]; 828c2ecf20Sopenharmony_ci u8 alarms; 838c2ecf20Sopenharmony_ci u8 beep_enable; 848c2ecf20Sopenharmony_ci u8 beep_mask; 858c2ecf20Sopenharmony_ci u8 alarm_mask; 868c2ecf20Sopenharmony_ci u8 two_temps; 878c2ecf20Sopenharmony_ci}; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci/* 908c2ecf20Sopenharmony_ci * Registers 0x07 to 0x0c are word-sized, others are byte-sized 918c2ecf20Sopenharmony_ci * GL520 uses a high-byte first convention 928c2ecf20Sopenharmony_ci */ 938c2ecf20Sopenharmony_cistatic int gl520_read_value(struct i2c_client *client, u8 reg) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci if ((reg >= 0x07) && (reg <= 0x0c)) 968c2ecf20Sopenharmony_ci return i2c_smbus_read_word_swapped(client, reg); 978c2ecf20Sopenharmony_ci else 988c2ecf20Sopenharmony_ci return i2c_smbus_read_byte_data(client, reg); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic int gl520_write_value(struct i2c_client *client, u8 reg, u16 value) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci if ((reg >= 0x07) && (reg <= 0x0c)) 1048c2ecf20Sopenharmony_ci return i2c_smbus_write_word_swapped(client, reg, value); 1058c2ecf20Sopenharmony_ci else 1068c2ecf20Sopenharmony_ci return i2c_smbus_write_byte_data(client, reg, value); 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic struct gl520_data *gl520_update_device(struct device *dev) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 1128c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 1138c2ecf20Sopenharmony_ci int val, i; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Starting gl520sm update\n"); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci data->alarms = gl520_read_value(client, GL520_REG_ALARMS); 1228c2ecf20Sopenharmony_ci data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); 1238c2ecf20Sopenharmony_ci data->vid = gl520_read_value(client, 1248c2ecf20Sopenharmony_ci GL520_REG_VID_INPUT) & 0x1f; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) { 1278c2ecf20Sopenharmony_ci data->in_input[i] = gl520_read_value(client, 1288c2ecf20Sopenharmony_ci GL520_REG_IN_INPUT[i]); 1298c2ecf20Sopenharmony_ci val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]); 1308c2ecf20Sopenharmony_ci data->in_min[i] = val & 0xff; 1318c2ecf20Sopenharmony_ci data->in_max[i] = (val >> 8) & 0xff; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci val = gl520_read_value(client, GL520_REG_FAN_INPUT); 1358c2ecf20Sopenharmony_ci data->fan_input[0] = (val >> 8) & 0xff; 1368c2ecf20Sopenharmony_ci data->fan_input[1] = val & 0xff; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci val = gl520_read_value(client, GL520_REG_FAN_MIN); 1398c2ecf20Sopenharmony_ci data->fan_min[0] = (val >> 8) & 0xff; 1408c2ecf20Sopenharmony_ci data->fan_min[1] = val & 0xff; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci data->temp_input[0] = gl520_read_value(client, 1438c2ecf20Sopenharmony_ci GL520_REG_TEMP_INPUT[0]); 1448c2ecf20Sopenharmony_ci data->temp_max[0] = gl520_read_value(client, 1458c2ecf20Sopenharmony_ci GL520_REG_TEMP_MAX[0]); 1468c2ecf20Sopenharmony_ci data->temp_max_hyst[0] = gl520_read_value(client, 1478c2ecf20Sopenharmony_ci GL520_REG_TEMP_MAX_HYST[0]); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci val = gl520_read_value(client, GL520_REG_FAN_DIV); 1508c2ecf20Sopenharmony_ci data->fan_div[0] = (val >> 6) & 0x03; 1518c2ecf20Sopenharmony_ci data->fan_div[1] = (val >> 4) & 0x03; 1528c2ecf20Sopenharmony_ci data->fan_off = (val >> 2) & 0x01; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci data->alarms &= data->alarm_mask; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci val = gl520_read_value(client, GL520_REG_CONF); 1578c2ecf20Sopenharmony_ci data->beep_enable = !((val >> 2) & 1); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* Temp1 and Vin4 are the same input */ 1608c2ecf20Sopenharmony_ci if (data->two_temps) { 1618c2ecf20Sopenharmony_ci data->temp_input[1] = gl520_read_value(client, 1628c2ecf20Sopenharmony_ci GL520_REG_TEMP_INPUT[1]); 1638c2ecf20Sopenharmony_ci data->temp_max[1] = gl520_read_value(client, 1648c2ecf20Sopenharmony_ci GL520_REG_TEMP_MAX[1]); 1658c2ecf20Sopenharmony_ci data->temp_max_hyst[1] = gl520_read_value(client, 1668c2ecf20Sopenharmony_ci GL520_REG_TEMP_MAX_HYST[1]); 1678c2ecf20Sopenharmony_ci } else { 1688c2ecf20Sopenharmony_ci data->in_input[4] = gl520_read_value(client, 1698c2ecf20Sopenharmony_ci GL520_REG_IN_INPUT[4]); 1708c2ecf20Sopenharmony_ci data->in_min[4] = gl520_read_value(client, 1718c2ecf20Sopenharmony_ci GL520_REG_IN_MIN[4]); 1728c2ecf20Sopenharmony_ci data->in_max[4] = gl520_read_value(client, 1738c2ecf20Sopenharmony_ci GL520_REG_IN_MAX[4]); 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci data->last_updated = jiffies; 1778c2ecf20Sopenharmony_ci data->valid = 1; 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci return data; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/* 1868c2ecf20Sopenharmony_ci * Sysfs stuff 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic ssize_t cpu0_vid_show(struct device *dev, 1908c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 1938c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci#define VDD_FROM_REG(val) DIV_ROUND_CLOSEST((val) * 95, 4) 1988c2ecf20Sopenharmony_ci#define VDD_CLAMP(val) clamp_val(val, 0, 255 * 95 / 4) 1998c2ecf20Sopenharmony_ci#define VDD_TO_REG(val) DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95) 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci#define IN_FROM_REG(val) ((val) * 19) 2028c2ecf20Sopenharmony_ci#define IN_CLAMP(val) clamp_val(val, 0, 255 * 19) 2038c2ecf20Sopenharmony_ci#define IN_TO_REG(val) DIV_ROUND_CLOSEST(IN_CLAMP(val), 19) 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic ssize_t in_input_show(struct device *dev, 2068c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 2098c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 2108c2ecf20Sopenharmony_ci u8 r = data->in_input[n]; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (n == 0) 2138c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", VDD_FROM_REG(r)); 2148c2ecf20Sopenharmony_ci else 2158c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(r)); 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic ssize_t in_min_show(struct device *dev, struct device_attribute *attr, 2198c2ecf20Sopenharmony_ci char *buf) 2208c2ecf20Sopenharmony_ci{ 2218c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 2228c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 2238c2ecf20Sopenharmony_ci u8 r = data->in_min[n]; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (n == 0) 2268c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", VDD_FROM_REG(r)); 2278c2ecf20Sopenharmony_ci else 2288c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(r)); 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cistatic ssize_t in_max_show(struct device *dev, struct device_attribute *attr, 2328c2ecf20Sopenharmony_ci char *buf) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 2358c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 2368c2ecf20Sopenharmony_ci u8 r = data->in_max[n]; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (n == 0) 2398c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", VDD_FROM_REG(r)); 2408c2ecf20Sopenharmony_ci else 2418c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(r)); 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, struct device_attribute *attr, 2458c2ecf20Sopenharmony_ci const char *buf, size_t count) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 2488c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 2498c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 2508c2ecf20Sopenharmony_ci u8 r; 2518c2ecf20Sopenharmony_ci long v; 2528c2ecf20Sopenharmony_ci int err; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &v); 2558c2ecf20Sopenharmony_ci if (err) 2568c2ecf20Sopenharmony_ci return err; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if (n == 0) 2618c2ecf20Sopenharmony_ci r = VDD_TO_REG(v); 2628c2ecf20Sopenharmony_ci else 2638c2ecf20Sopenharmony_ci r = IN_TO_REG(v); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci data->in_min[n] = r; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (n < 4) 2688c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_IN_MIN[n], 2698c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_IN_MIN[n]) 2708c2ecf20Sopenharmony_ci & ~0xff) | r); 2718c2ecf20Sopenharmony_ci else 2728c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_IN_MIN[n], r); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 2758c2ecf20Sopenharmony_ci return count; 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, struct device_attribute *attr, 2798c2ecf20Sopenharmony_ci const char *buf, size_t count) 2808c2ecf20Sopenharmony_ci{ 2818c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 2828c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 2838c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 2848c2ecf20Sopenharmony_ci u8 r; 2858c2ecf20Sopenharmony_ci long v; 2868c2ecf20Sopenharmony_ci int err; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &v); 2898c2ecf20Sopenharmony_ci if (err) 2908c2ecf20Sopenharmony_ci return err; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci if (n == 0) 2938c2ecf20Sopenharmony_ci r = VDD_TO_REG(v); 2948c2ecf20Sopenharmony_ci else 2958c2ecf20Sopenharmony_ci r = IN_TO_REG(v); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci data->in_max[n] = r; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci if (n < 4) 3028c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_IN_MAX[n], 3038c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_IN_MAX[n]) 3048c2ecf20Sopenharmony_ci & ~0xff00) | (r << 8)); 3058c2ecf20Sopenharmony_ci else 3068c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_IN_MAX[n], r); 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 3098c2ecf20Sopenharmony_ci return count; 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in_input, 0); 3138c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1); 3148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2); 3158c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3); 3168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4); 3178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0); 3188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1); 3198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2); 3208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3); 3218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4); 3228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0); 3238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1); 3248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2); 3258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3); 3268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val)) 3298c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div)))) 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci#define FAN_BASE(div) (480000 >> (div)) 3328c2ecf20Sopenharmony_ci#define FAN_CLAMP(val, div) clamp_val(val, FAN_BASE(div) / 255, \ 3338c2ecf20Sopenharmony_ci FAN_BASE(div)) 3348c2ecf20Sopenharmony_ci#define FAN_TO_REG(val, div) ((val) == 0 ? 0 : \ 3358c2ecf20Sopenharmony_ci DIV_ROUND_CLOSEST(480000, \ 3368c2ecf20Sopenharmony_ci FAN_CLAMP(val, div) << (div))) 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic ssize_t fan_input_show(struct device *dev, 3398c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 3428c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n], 3458c2ecf20Sopenharmony_ci data->fan_div[n])); 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *attr, 3498c2ecf20Sopenharmony_ci char *buf) 3508c2ecf20Sopenharmony_ci{ 3518c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 3528c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n], 3558c2ecf20Sopenharmony_ci data->fan_div[n])); 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_cistatic ssize_t fan_div_show(struct device *dev, struct device_attribute *attr, 3598c2ecf20Sopenharmony_ci char *buf) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 3628c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n])); 3658c2ecf20Sopenharmony_ci} 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_cistatic ssize_t fan1_off_show(struct device *dev, 3688c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3698c2ecf20Sopenharmony_ci{ 3708c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 3718c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->fan_off); 3728c2ecf20Sopenharmony_ci} 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev, 3758c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 3768c2ecf20Sopenharmony_ci size_t count) 3778c2ecf20Sopenharmony_ci{ 3788c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 3798c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 3808c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 3818c2ecf20Sopenharmony_ci u8 r; 3828c2ecf20Sopenharmony_ci unsigned long v; 3838c2ecf20Sopenharmony_ci int err; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &v); 3868c2ecf20Sopenharmony_ci if (err) 3878c2ecf20Sopenharmony_ci return err; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 3908c2ecf20Sopenharmony_ci r = FAN_TO_REG(v, data->fan_div[n]); 3918c2ecf20Sopenharmony_ci data->fan_min[n] = r; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci if (n == 0) 3948c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_FAN_MIN, 3958c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_FAN_MIN) 3968c2ecf20Sopenharmony_ci & ~0xff00) | (r << 8)); 3978c2ecf20Sopenharmony_ci else 3988c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_FAN_MIN, 3998c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_FAN_MIN) 4008c2ecf20Sopenharmony_ci & ~0xff) | r); 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); 4038c2ecf20Sopenharmony_ci if (data->fan_min[n] == 0) 4048c2ecf20Sopenharmony_ci data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40; 4058c2ecf20Sopenharmony_ci else 4068c2ecf20Sopenharmony_ci data->alarm_mask |= (n == 0) ? 0x20 : 0x40; 4078c2ecf20Sopenharmony_ci data->beep_mask &= data->alarm_mask; 4088c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4118c2ecf20Sopenharmony_ci return count; 4128c2ecf20Sopenharmony_ci} 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cistatic ssize_t fan_div_store(struct device *dev, 4158c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 4168c2ecf20Sopenharmony_ci size_t count) 4178c2ecf20Sopenharmony_ci{ 4188c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 4198c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 4208c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 4218c2ecf20Sopenharmony_ci u8 r; 4228c2ecf20Sopenharmony_ci unsigned long v; 4238c2ecf20Sopenharmony_ci int err; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &v); 4268c2ecf20Sopenharmony_ci if (err) 4278c2ecf20Sopenharmony_ci return err; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci switch (v) { 4308c2ecf20Sopenharmony_ci case 1: 4318c2ecf20Sopenharmony_ci r = 0; 4328c2ecf20Sopenharmony_ci break; 4338c2ecf20Sopenharmony_ci case 2: 4348c2ecf20Sopenharmony_ci r = 1; 4358c2ecf20Sopenharmony_ci break; 4368c2ecf20Sopenharmony_ci case 4: 4378c2ecf20Sopenharmony_ci r = 2; 4388c2ecf20Sopenharmony_ci break; 4398c2ecf20Sopenharmony_ci case 8: 4408c2ecf20Sopenharmony_ci r = 3; 4418c2ecf20Sopenharmony_ci break; 4428c2ecf20Sopenharmony_ci default: 4438c2ecf20Sopenharmony_ci dev_err(&client->dev, 4448c2ecf20Sopenharmony_ci "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v); 4458c2ecf20Sopenharmony_ci return -EINVAL; 4468c2ecf20Sopenharmony_ci } 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4498c2ecf20Sopenharmony_ci data->fan_div[n] = r; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci if (n == 0) 4528c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_FAN_DIV, 4538c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_FAN_DIV) 4548c2ecf20Sopenharmony_ci & ~0xc0) | (r << 6)); 4558c2ecf20Sopenharmony_ci else 4568c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_FAN_DIV, 4578c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_FAN_DIV) 4588c2ecf20Sopenharmony_ci & ~0x30) | (r << 4)); 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4618c2ecf20Sopenharmony_ci return count; 4628c2ecf20Sopenharmony_ci} 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic ssize_t fan1_off_store(struct device *dev, 4658c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 4668c2ecf20Sopenharmony_ci size_t count) 4678c2ecf20Sopenharmony_ci{ 4688c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 4698c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 4708c2ecf20Sopenharmony_ci u8 r; 4718c2ecf20Sopenharmony_ci unsigned long v; 4728c2ecf20Sopenharmony_ci int err; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &v); 4758c2ecf20Sopenharmony_ci if (err) 4768c2ecf20Sopenharmony_ci return err; 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci r = (v ? 1 : 0); 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4818c2ecf20Sopenharmony_ci data->fan_off = r; 4828c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_FAN_OFF, 4838c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_FAN_OFF) 4848c2ecf20Sopenharmony_ci & ~0x0c) | (r << 2)); 4858c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4868c2ecf20Sopenharmony_ci return count; 4878c2ecf20Sopenharmony_ci} 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); 4908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); 4918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); 4928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); 4938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0); 4948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1); 4958c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(fan1_off); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci#define TEMP_FROM_REG(val) (((val) - 130) * 1000) 4988c2ecf20Sopenharmony_ci#define TEMP_CLAMP(val) clamp_val(val, -130000, 125000) 4998c2ecf20Sopenharmony_ci#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 130) 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_cistatic ssize_t temp_input_show(struct device *dev, 5028c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 5038c2ecf20Sopenharmony_ci{ 5048c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 5058c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n])); 5088c2ecf20Sopenharmony_ci} 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_cistatic ssize_t temp_max_show(struct device *dev, 5118c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 5128c2ecf20Sopenharmony_ci{ 5138c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 5148c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n])); 5178c2ecf20Sopenharmony_ci} 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic ssize_t temp_max_hyst_show(struct device *dev, 5208c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 5218c2ecf20Sopenharmony_ci{ 5228c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 5238c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n])); 5268c2ecf20Sopenharmony_ci} 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev, 5298c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 5308c2ecf20Sopenharmony_ci size_t count) 5318c2ecf20Sopenharmony_ci{ 5328c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 5338c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 5348c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 5358c2ecf20Sopenharmony_ci long v; 5368c2ecf20Sopenharmony_ci int err; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &v); 5398c2ecf20Sopenharmony_ci if (err) 5408c2ecf20Sopenharmony_ci return err; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5438c2ecf20Sopenharmony_ci data->temp_max[n] = TEMP_TO_REG(v); 5448c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]); 5458c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 5468c2ecf20Sopenharmony_ci return count; 5478c2ecf20Sopenharmony_ci} 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_cistatic ssize_t temp_max_hyst_store(struct device *dev, 5508c2ecf20Sopenharmony_ci struct device_attribute *attr, 5518c2ecf20Sopenharmony_ci const char *buf, size_t count) 5528c2ecf20Sopenharmony_ci{ 5538c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 5548c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 5558c2ecf20Sopenharmony_ci int n = to_sensor_dev_attr(attr)->index; 5568c2ecf20Sopenharmony_ci long v; 5578c2ecf20Sopenharmony_ci int err; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &v); 5608c2ecf20Sopenharmony_ci if (err) 5618c2ecf20Sopenharmony_ci return err; 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5648c2ecf20Sopenharmony_ci data->temp_max_hyst[n] = TEMP_TO_REG(v); 5658c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n], 5668c2ecf20Sopenharmony_ci data->temp_max_hyst[n]); 5678c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 5688c2ecf20Sopenharmony_ci return count; 5698c2ecf20Sopenharmony_ci} 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); 5728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); 5738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); 5748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); 5758c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0); 5768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1); 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *attr, 5798c2ecf20Sopenharmony_ci char *buf) 5808c2ecf20Sopenharmony_ci{ 5818c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 5828c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->alarms); 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_cistatic ssize_t beep_enable_show(struct device *dev, 5868c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 5878c2ecf20Sopenharmony_ci{ 5888c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 5898c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->beep_enable); 5908c2ecf20Sopenharmony_ci} 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_cistatic ssize_t beep_mask_show(struct device *dev, 5938c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 5948c2ecf20Sopenharmony_ci{ 5958c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 5968c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->beep_mask); 5978c2ecf20Sopenharmony_ci} 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_cistatic ssize_t beep_enable_store(struct device *dev, 6008c2ecf20Sopenharmony_ci struct device_attribute *attr, 6018c2ecf20Sopenharmony_ci const char *buf, size_t count) 6028c2ecf20Sopenharmony_ci{ 6038c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 6048c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 6058c2ecf20Sopenharmony_ci u8 r; 6068c2ecf20Sopenharmony_ci unsigned long v; 6078c2ecf20Sopenharmony_ci int err; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &v); 6108c2ecf20Sopenharmony_ci if (err) 6118c2ecf20Sopenharmony_ci return err; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci r = (v ? 0 : 1); 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6168c2ecf20Sopenharmony_ci data->beep_enable = !r; 6178c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_BEEP_ENABLE, 6188c2ecf20Sopenharmony_ci (gl520_read_value(client, GL520_REG_BEEP_ENABLE) 6198c2ecf20Sopenharmony_ci & ~0x04) | (r << 2)); 6208c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6218c2ecf20Sopenharmony_ci return count; 6228c2ecf20Sopenharmony_ci} 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_cistatic ssize_t beep_mask_store(struct device *dev, 6258c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 6268c2ecf20Sopenharmony_ci size_t count) 6278c2ecf20Sopenharmony_ci{ 6288c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 6298c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 6308c2ecf20Sopenharmony_ci unsigned long r; 6318c2ecf20Sopenharmony_ci int err; 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &r); 6348c2ecf20Sopenharmony_ci if (err) 6358c2ecf20Sopenharmony_ci return err; 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6388c2ecf20Sopenharmony_ci r &= data->alarm_mask; 6398c2ecf20Sopenharmony_ci data->beep_mask = r; 6408c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_BEEP_MASK, r); 6418c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6428c2ecf20Sopenharmony_ci return count; 6438c2ecf20Sopenharmony_ci} 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms); 6468c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(beep_enable); 6478c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(beep_mask); 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr, 6508c2ecf20Sopenharmony_ci char *buf) 6518c2ecf20Sopenharmony_ci{ 6528c2ecf20Sopenharmony_ci int bit_nr = to_sensor_dev_attr(attr)->index; 6538c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1); 6568c2ecf20Sopenharmony_ci} 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); 6598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); 6608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); 6618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); 6628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4); 6638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5); 6648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6); 6658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 7); 6668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 7); 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_cistatic ssize_t beep_show(struct device *dev, struct device_attribute *attr, 6698c2ecf20Sopenharmony_ci char *buf) 6708c2ecf20Sopenharmony_ci{ 6718c2ecf20Sopenharmony_ci int bitnr = to_sensor_dev_attr(attr)->index; 6728c2ecf20Sopenharmony_ci struct gl520_data *data = gl520_update_device(dev); 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1); 6758c2ecf20Sopenharmony_ci} 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_cistatic ssize_t beep_store(struct device *dev, struct device_attribute *attr, 6788c2ecf20Sopenharmony_ci const char *buf, size_t count) 6798c2ecf20Sopenharmony_ci{ 6808c2ecf20Sopenharmony_ci struct gl520_data *data = dev_get_drvdata(dev); 6818c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 6828c2ecf20Sopenharmony_ci int bitnr = to_sensor_dev_attr(attr)->index; 6838c2ecf20Sopenharmony_ci unsigned long bit; 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci int err; 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &bit); 6888c2ecf20Sopenharmony_ci if (err) 6898c2ecf20Sopenharmony_ci return err; 6908c2ecf20Sopenharmony_ci if (bit & ~1) 6918c2ecf20Sopenharmony_ci return -EINVAL; 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 6948c2ecf20Sopenharmony_ci data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); 6958c2ecf20Sopenharmony_ci if (bit) 6968c2ecf20Sopenharmony_ci data->beep_mask |= (1 << bitnr); 6978c2ecf20Sopenharmony_ci else 6988c2ecf20Sopenharmony_ci data->beep_mask &= ~(1 << bitnr); 6998c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); 7008c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 7018c2ecf20Sopenharmony_ci return count; 7028c2ecf20Sopenharmony_ci} 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0); 7058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1); 7068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2); 7078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3); 7088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4); 7098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5); 7108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6); 7118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 7); 7128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 7); 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_cistatic struct attribute *gl520_attributes[] = { 7158c2ecf20Sopenharmony_ci &dev_attr_cpu0_vid.attr, 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_input.dev_attr.attr, 7188c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_min.dev_attr.attr, 7198c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_max.dev_attr.attr, 7208c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_alarm.dev_attr.attr, 7218c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_beep.dev_attr.attr, 7228c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_input.dev_attr.attr, 7238c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_min.dev_attr.attr, 7248c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_max.dev_attr.attr, 7258c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_alarm.dev_attr.attr, 7268c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_beep.dev_attr.attr, 7278c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_input.dev_attr.attr, 7288c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_min.dev_attr.attr, 7298c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_max.dev_attr.attr, 7308c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_alarm.dev_attr.attr, 7318c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_beep.dev_attr.attr, 7328c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_input.dev_attr.attr, 7338c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_min.dev_attr.attr, 7348c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_max.dev_attr.attr, 7358c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_alarm.dev_attr.attr, 7368c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_beep.dev_attr.attr, 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_input.dev_attr.attr, 7398c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_min.dev_attr.attr, 7408c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_div.dev_attr.attr, 7418c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_alarm.dev_attr.attr, 7428c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_beep.dev_attr.attr, 7438c2ecf20Sopenharmony_ci &dev_attr_fan1_off.attr, 7448c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_input.dev_attr.attr, 7458c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_min.dev_attr.attr, 7468c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_div.dev_attr.attr, 7478c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_alarm.dev_attr.attr, 7488c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_beep.dev_attr.attr, 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 7518c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max.dev_attr.attr, 7528c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 7538c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_alarm.dev_attr.attr, 7548c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_beep.dev_attr.attr, 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 7578c2ecf20Sopenharmony_ci &dev_attr_beep_enable.attr, 7588c2ecf20Sopenharmony_ci &dev_attr_beep_mask.attr, 7598c2ecf20Sopenharmony_ci NULL 7608c2ecf20Sopenharmony_ci}; 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_cistatic const struct attribute_group gl520_group = { 7638c2ecf20Sopenharmony_ci .attrs = gl520_attributes, 7648c2ecf20Sopenharmony_ci}; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_cistatic struct attribute *gl520_attributes_in4[] = { 7678c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_input.dev_attr.attr, 7688c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_min.dev_attr.attr, 7698c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_max.dev_attr.attr, 7708c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_alarm.dev_attr.attr, 7718c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_beep.dev_attr.attr, 7728c2ecf20Sopenharmony_ci NULL 7738c2ecf20Sopenharmony_ci}; 7748c2ecf20Sopenharmony_ci 7758c2ecf20Sopenharmony_cistatic struct attribute *gl520_attributes_temp2[] = { 7768c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_input.dev_attr.attr, 7778c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_max.dev_attr.attr, 7788c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, 7798c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_alarm.dev_attr.attr, 7808c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_beep.dev_attr.attr, 7818c2ecf20Sopenharmony_ci NULL 7828c2ecf20Sopenharmony_ci}; 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_cistatic const struct attribute_group gl520_group_in4 = { 7858c2ecf20Sopenharmony_ci .attrs = gl520_attributes_in4, 7868c2ecf20Sopenharmony_ci}; 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_cistatic const struct attribute_group gl520_group_temp2 = { 7898c2ecf20Sopenharmony_ci .attrs = gl520_attributes_temp2, 7908c2ecf20Sopenharmony_ci}; 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci/* 7948c2ecf20Sopenharmony_ci * Real code 7958c2ecf20Sopenharmony_ci */ 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */ 7988c2ecf20Sopenharmony_cistatic int gl520_detect(struct i2c_client *client, struct i2c_board_info *info) 7998c2ecf20Sopenharmony_ci{ 8008c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 8038c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_WORD_DATA)) 8048c2ecf20Sopenharmony_ci return -ENODEV; 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci /* Determine the chip type. */ 8078c2ecf20Sopenharmony_ci if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) || 8088c2ecf20Sopenharmony_ci ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) || 8098c2ecf20Sopenharmony_ci ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) { 8108c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Unknown chip type, skipping\n"); 8118c2ecf20Sopenharmony_ci return -ENODEV; 8128c2ecf20Sopenharmony_ci } 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci strlcpy(info->type, "gl520sm", I2C_NAME_SIZE); 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci return 0; 8178c2ecf20Sopenharmony_ci} 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci/* Called when we have found a new GL520SM. */ 8208c2ecf20Sopenharmony_cistatic void gl520_init_client(struct i2c_client *client) 8218c2ecf20Sopenharmony_ci{ 8228c2ecf20Sopenharmony_ci struct gl520_data *data = i2c_get_clientdata(client); 8238c2ecf20Sopenharmony_ci u8 oldconf, conf; 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci conf = oldconf = gl520_read_value(client, GL520_REG_CONF); 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci data->alarm_mask = 0xff; 8288c2ecf20Sopenharmony_ci data->vrm = vid_which_vrm(); 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci if (extra_sensor_type == 1) 8318c2ecf20Sopenharmony_ci conf &= ~0x10; 8328c2ecf20Sopenharmony_ci else if (extra_sensor_type == 2) 8338c2ecf20Sopenharmony_ci conf |= 0x10; 8348c2ecf20Sopenharmony_ci data->two_temps = !(conf & 0x10); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci /* If IRQ# is disabled, we can safely force comparator mode */ 8378c2ecf20Sopenharmony_ci if (!(conf & 0x20)) 8388c2ecf20Sopenharmony_ci conf &= 0xf7; 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci /* Enable monitoring if needed */ 8418c2ecf20Sopenharmony_ci conf |= 0x40; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci if (conf != oldconf) 8448c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_CONF, conf); 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci gl520_update_device(&(client->dev)); 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci if (data->fan_min[0] == 0) 8498c2ecf20Sopenharmony_ci data->alarm_mask &= ~0x20; 8508c2ecf20Sopenharmony_ci if (data->fan_min[1] == 0) 8518c2ecf20Sopenharmony_ci data->alarm_mask &= ~0x40; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci data->beep_mask &= data->alarm_mask; 8548c2ecf20Sopenharmony_ci gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); 8558c2ecf20Sopenharmony_ci} 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_cistatic int gl520_probe(struct i2c_client *client) 8588c2ecf20Sopenharmony_ci{ 8598c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 8608c2ecf20Sopenharmony_ci struct device *hwmon_dev; 8618c2ecf20Sopenharmony_ci struct gl520_data *data; 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(struct gl520_data), GFP_KERNEL); 8648c2ecf20Sopenharmony_ci if (!data) 8658c2ecf20Sopenharmony_ci return -ENOMEM; 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci i2c_set_clientdata(client, data); 8688c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 8698c2ecf20Sopenharmony_ci data->client = client; 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci /* Initialize the GL520SM chip */ 8728c2ecf20Sopenharmony_ci gl520_init_client(client); 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci /* sysfs hooks */ 8758c2ecf20Sopenharmony_ci data->groups[0] = &gl520_group; 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci if (data->two_temps) 8788c2ecf20Sopenharmony_ci data->groups[1] = &gl520_group_temp2; 8798c2ecf20Sopenharmony_ci else 8808c2ecf20Sopenharmony_ci data->groups[1] = &gl520_group_in4; 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 8838c2ecf20Sopenharmony_ci data, data->groups); 8848c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 8858c2ecf20Sopenharmony_ci} 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_cistatic const struct i2c_device_id gl520_id[] = { 8888c2ecf20Sopenharmony_ci { "gl520sm", 0 }, 8898c2ecf20Sopenharmony_ci { } 8908c2ecf20Sopenharmony_ci}; 8918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, gl520_id); 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_cistatic struct i2c_driver gl520_driver = { 8948c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 8958c2ecf20Sopenharmony_ci .driver = { 8968c2ecf20Sopenharmony_ci .name = "gl520sm", 8978c2ecf20Sopenharmony_ci }, 8988c2ecf20Sopenharmony_ci .probe_new = gl520_probe, 8998c2ecf20Sopenharmony_ci .id_table = gl520_id, 9008c2ecf20Sopenharmony_ci .detect = gl520_detect, 9018c2ecf20Sopenharmony_ci .address_list = normal_i2c, 9028c2ecf20Sopenharmony_ci}; 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_cimodule_i2c_driver(gl520_driver); 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " 9078c2ecf20Sopenharmony_ci "Kyösti Mälkki <kmalkki@cc.hut.fi>, " 9088c2ecf20Sopenharmony_ci "Maarten Deprez <maartendeprez@users.sourceforge.net>"); 9098c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GL520SM driver"); 9108c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 911