18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware 48c2ecf20Sopenharmony_ci * monitoring 58c2ecf20Sopenharmony_ci * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and 68c2ecf20Sopenharmony_ci * Kyosti Malkki <kmalkki@cc.hut.fi> 78c2ecf20Sopenharmony_ci * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and 88c2ecf20Sopenharmony_ci * Jean Delvare <jdelvare@suse.de> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare 118c2ecf20Sopenharmony_ci * and advice of Greg Kroah-Hartman. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * Notes about the port: 148c2ecf20Sopenharmony_ci * Release 0x00 of the GL518SM chipset doesn't support reading of in0, 158c2ecf20Sopenharmony_ci * in1 nor in2. The original driver had an ugly workaround to get them 168c2ecf20Sopenharmony_ci * anyway (changing limits and watching alarms trigger and wear off). 178c2ecf20Sopenharmony_ci * We did not keep that part of the original driver in the Linux 2.6 188c2ecf20Sopenharmony_ci * version, since it was making the driver significantly more complex 198c2ecf20Sopenharmony_ci * with no real benefit. 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <linux/module.h> 238c2ecf20Sopenharmony_ci#include <linux/init.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 268c2ecf20Sopenharmony_ci#include <linux/i2c.h> 278c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 288c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 298c2ecf20Sopenharmony_ci#include <linux/err.h> 308c2ecf20Sopenharmony_ci#include <linux/mutex.h> 318c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* Addresses to scan */ 348c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cienum chips { gl518sm_r00, gl518sm_r80 }; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* Many GL518 constants specified below */ 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* The GL518 registers */ 418c2ecf20Sopenharmony_ci#define GL518_REG_CHIP_ID 0x00 428c2ecf20Sopenharmony_ci#define GL518_REG_REVISION 0x01 438c2ecf20Sopenharmony_ci#define GL518_REG_VENDOR_ID 0x02 448c2ecf20Sopenharmony_ci#define GL518_REG_CONF 0x03 458c2ecf20Sopenharmony_ci#define GL518_REG_TEMP_IN 0x04 468c2ecf20Sopenharmony_ci#define GL518_REG_TEMP_MAX 0x05 478c2ecf20Sopenharmony_ci#define GL518_REG_TEMP_HYST 0x06 488c2ecf20Sopenharmony_ci#define GL518_REG_FAN_COUNT 0x07 498c2ecf20Sopenharmony_ci#define GL518_REG_FAN_LIMIT 0x08 508c2ecf20Sopenharmony_ci#define GL518_REG_VIN1_LIMIT 0x09 518c2ecf20Sopenharmony_ci#define GL518_REG_VIN2_LIMIT 0x0a 528c2ecf20Sopenharmony_ci#define GL518_REG_VIN3_LIMIT 0x0b 538c2ecf20Sopenharmony_ci#define GL518_REG_VDD_LIMIT 0x0c 548c2ecf20Sopenharmony_ci#define GL518_REG_VIN3 0x0d 558c2ecf20Sopenharmony_ci#define GL518_REG_MISC 0x0f 568c2ecf20Sopenharmony_ci#define GL518_REG_ALARM 0x10 578c2ecf20Sopenharmony_ci#define GL518_REG_MASK 0x11 588c2ecf20Sopenharmony_ci#define GL518_REG_INT 0x12 598c2ecf20Sopenharmony_ci#define GL518_REG_VIN2 0x13 608c2ecf20Sopenharmony_ci#define GL518_REG_VIN1 0x14 618c2ecf20Sopenharmony_ci#define GL518_REG_VDD 0x15 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/* 658c2ecf20Sopenharmony_ci * Conversions. Rounding and limit checking is only done on the TO_REG 668c2ecf20Sopenharmony_ci * variants. Note that you should be a bit careful with which arguments 678c2ecf20Sopenharmony_ci * these macros are called: arguments may be evaluated more than once. 688c2ecf20Sopenharmony_ci * Fixing this is just not worth it. 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#define RAW_FROM_REG(val) val 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci#define BOOL_FROM_REG(val) ((val) ? 0 : 1) 748c2ecf20Sopenharmony_ci#define BOOL_TO_REG(val) ((val) ? 0 : 1) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#define TEMP_CLAMP(val) clamp_val(val, -119000, 136000) 778c2ecf20Sopenharmony_ci#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 119) 788c2ecf20Sopenharmony_ci#define TEMP_FROM_REG(val) (((val) - 119) * 1000) 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic inline u8 FAN_TO_REG(long rpm, int div) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci long rpmdiv; 838c2ecf20Sopenharmony_ci if (rpm == 0) 848c2ecf20Sopenharmony_ci return 0; 858c2ecf20Sopenharmony_ci rpmdiv = clamp_val(rpm, 1, 960000) * div; 868c2ecf20Sopenharmony_ci return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255); 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) * (div)))) 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define IN_CLAMP(val) clamp_val(val, 0, 255 * 19) 918c2ecf20Sopenharmony_ci#define IN_TO_REG(val) DIV_ROUND_CLOSEST(IN_CLAMP(val), 19) 928c2ecf20Sopenharmony_ci#define IN_FROM_REG(val) ((val) * 19) 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define VDD_CLAMP(val) clamp_val(val, 0, 255 * 95 / 4) 958c2ecf20Sopenharmony_ci#define VDD_TO_REG(val) DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95) 968c2ecf20Sopenharmony_ci#define VDD_FROM_REG(val) DIV_ROUND_CLOSEST((val) * 95, 4) 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val)) 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask) 1018c2ecf20Sopenharmony_ci#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f) 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/* Each client has this additional data */ 1048c2ecf20Sopenharmony_cistruct gl518_data { 1058c2ecf20Sopenharmony_ci struct i2c_client *client; 1068c2ecf20Sopenharmony_ci const struct attribute_group *groups[3]; 1078c2ecf20Sopenharmony_ci enum chips type; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci struct mutex update_lock; 1108c2ecf20Sopenharmony_ci char valid; /* !=0 if following fields are valid */ 1118c2ecf20Sopenharmony_ci unsigned long last_updated; /* In jiffies */ 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci u8 voltage_in[4]; /* Register values; [0] = VDD */ 1148c2ecf20Sopenharmony_ci u8 voltage_min[4]; /* Register values; [0] = VDD */ 1158c2ecf20Sopenharmony_ci u8 voltage_max[4]; /* Register values; [0] = VDD */ 1168c2ecf20Sopenharmony_ci u8 fan_in[2]; 1178c2ecf20Sopenharmony_ci u8 fan_min[2]; 1188c2ecf20Sopenharmony_ci u8 fan_div[2]; /* Register encoding, shifted right */ 1198c2ecf20Sopenharmony_ci u8 fan_auto1; /* Boolean */ 1208c2ecf20Sopenharmony_ci u8 temp_in; /* Register values */ 1218c2ecf20Sopenharmony_ci u8 temp_max; /* Register values */ 1228c2ecf20Sopenharmony_ci u8 temp_hyst; /* Register values */ 1238c2ecf20Sopenharmony_ci u8 alarms; /* Register value */ 1248c2ecf20Sopenharmony_ci u8 alarm_mask; 1258c2ecf20Sopenharmony_ci u8 beep_mask; /* Register value */ 1268c2ecf20Sopenharmony_ci u8 beep_enable; /* Boolean */ 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* 1308c2ecf20Sopenharmony_ci * Registers 0x07 to 0x0c are word-sized, others are byte-sized 1318c2ecf20Sopenharmony_ci * GL518 uses a high-byte first convention, which is exactly opposite to 1328c2ecf20Sopenharmony_ci * the SMBus standard. 1338c2ecf20Sopenharmony_ci */ 1348c2ecf20Sopenharmony_cistatic int gl518_read_value(struct i2c_client *client, u8 reg) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci if ((reg >= 0x07) && (reg <= 0x0c)) 1378c2ecf20Sopenharmony_ci return i2c_smbus_read_word_swapped(client, reg); 1388c2ecf20Sopenharmony_ci else 1398c2ecf20Sopenharmony_ci return i2c_smbus_read_byte_data(client, reg); 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci if ((reg >= 0x07) && (reg <= 0x0c)) 1458c2ecf20Sopenharmony_ci return i2c_smbus_write_word_swapped(client, reg, value); 1468c2ecf20Sopenharmony_ci else 1478c2ecf20Sopenharmony_ci return i2c_smbus_write_byte_data(client, reg, value); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic struct gl518_data *gl518_update_device(struct device *dev) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci struct gl518_data *data = dev_get_drvdata(dev); 1538c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 1548c2ecf20Sopenharmony_ci int val; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated + HZ + HZ / 2) 1598c2ecf20Sopenharmony_ci || !data->valid) { 1608c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Starting gl518 update\n"); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci data->alarms = gl518_read_value(client, GL518_REG_INT); 1638c2ecf20Sopenharmony_ci data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_VDD_LIMIT); 1668c2ecf20Sopenharmony_ci data->voltage_min[0] = val & 0xff; 1678c2ecf20Sopenharmony_ci data->voltage_max[0] = (val >> 8) & 0xff; 1688c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); 1698c2ecf20Sopenharmony_ci data->voltage_min[1] = val & 0xff; 1708c2ecf20Sopenharmony_ci data->voltage_max[1] = (val >> 8) & 0xff; 1718c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); 1728c2ecf20Sopenharmony_ci data->voltage_min[2] = val & 0xff; 1738c2ecf20Sopenharmony_ci data->voltage_max[2] = (val >> 8) & 0xff; 1748c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); 1758c2ecf20Sopenharmony_ci data->voltage_min[3] = val & 0xff; 1768c2ecf20Sopenharmony_ci data->voltage_max[3] = (val >> 8) & 0xff; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_FAN_COUNT); 1798c2ecf20Sopenharmony_ci data->fan_in[0] = (val >> 8) & 0xff; 1808c2ecf20Sopenharmony_ci data->fan_in[1] = val & 0xff; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_FAN_LIMIT); 1838c2ecf20Sopenharmony_ci data->fan_min[0] = (val >> 8) & 0xff; 1848c2ecf20Sopenharmony_ci data->fan_min[1] = val & 0xff; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); 1878c2ecf20Sopenharmony_ci data->temp_max = 1888c2ecf20Sopenharmony_ci gl518_read_value(client, GL518_REG_TEMP_MAX); 1898c2ecf20Sopenharmony_ci data->temp_hyst = 1908c2ecf20Sopenharmony_ci gl518_read_value(client, GL518_REG_TEMP_HYST); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_MISC); 1938c2ecf20Sopenharmony_ci data->fan_div[0] = (val >> 6) & 0x03; 1948c2ecf20Sopenharmony_ci data->fan_div[1] = (val >> 4) & 0x03; 1958c2ecf20Sopenharmony_ci data->fan_auto1 = (val >> 3) & 0x01; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci data->alarms &= data->alarm_mask; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci val = gl518_read_value(client, GL518_REG_CONF); 2008c2ecf20Sopenharmony_ci data->beep_enable = (val >> 2) & 1; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci if (data->type != gl518sm_r00) { 2038c2ecf20Sopenharmony_ci data->voltage_in[0] = 2048c2ecf20Sopenharmony_ci gl518_read_value(client, GL518_REG_VDD); 2058c2ecf20Sopenharmony_ci data->voltage_in[1] = 2068c2ecf20Sopenharmony_ci gl518_read_value(client, GL518_REG_VIN1); 2078c2ecf20Sopenharmony_ci data->voltage_in[2] = 2088c2ecf20Sopenharmony_ci gl518_read_value(client, GL518_REG_VIN2); 2098c2ecf20Sopenharmony_ci } 2108c2ecf20Sopenharmony_ci data->voltage_in[3] = 2118c2ecf20Sopenharmony_ci gl518_read_value(client, GL518_REG_VIN3); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci data->last_updated = jiffies; 2148c2ecf20Sopenharmony_ci data->valid = 1; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci return data; 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci/* 2238c2ecf20Sopenharmony_ci * Sysfs stuff 2248c2ecf20Sopenharmony_ci */ 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci#define show(type, suffix, value) \ 2278c2ecf20Sopenharmony_cistatic ssize_t show_##suffix(struct device *dev, \ 2288c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) \ 2298c2ecf20Sopenharmony_ci{ \ 2308c2ecf20Sopenharmony_ci struct gl518_data *data = gl518_update_device(dev); \ 2318c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cishow(TEMP, temp_input1, temp_in); 2358c2ecf20Sopenharmony_cishow(TEMP, temp_max1, temp_max); 2368c2ecf20Sopenharmony_cishow(TEMP, temp_hyst1, temp_hyst); 2378c2ecf20Sopenharmony_cishow(BOOL, fan_auto1, fan_auto1); 2388c2ecf20Sopenharmony_cishow(VDD, in_input0, voltage_in[0]); 2398c2ecf20Sopenharmony_cishow(IN, in_input1, voltage_in[1]); 2408c2ecf20Sopenharmony_cishow(IN, in_input2, voltage_in[2]); 2418c2ecf20Sopenharmony_cishow(IN, in_input3, voltage_in[3]); 2428c2ecf20Sopenharmony_cishow(VDD, in_min0, voltage_min[0]); 2438c2ecf20Sopenharmony_cishow(IN, in_min1, voltage_min[1]); 2448c2ecf20Sopenharmony_cishow(IN, in_min2, voltage_min[2]); 2458c2ecf20Sopenharmony_cishow(IN, in_min3, voltage_min[3]); 2468c2ecf20Sopenharmony_cishow(VDD, in_max0, voltage_max[0]); 2478c2ecf20Sopenharmony_cishow(IN, in_max1, voltage_max[1]); 2488c2ecf20Sopenharmony_cishow(IN, in_max2, voltage_max[2]); 2498c2ecf20Sopenharmony_cishow(IN, in_max3, voltage_max[3]); 2508c2ecf20Sopenharmony_cishow(RAW, alarms, alarms); 2518c2ecf20Sopenharmony_cishow(BOOL, beep_enable, beep_enable); 2528c2ecf20Sopenharmony_cishow(BEEP_MASK, beep_mask, beep_mask); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic ssize_t fan_input_show(struct device *dev, 2558c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr(attr)->index; 2588c2ecf20Sopenharmony_ci struct gl518_data *data = gl518_update_device(dev); 2598c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr], 2608c2ecf20Sopenharmony_ci DIV_FROM_REG(data->fan_div[nr]))); 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *attr, 2648c2ecf20Sopenharmony_ci char *buf) 2658c2ecf20Sopenharmony_ci{ 2668c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr(attr)->index; 2678c2ecf20Sopenharmony_ci struct gl518_data *data = gl518_update_device(dev); 2688c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], 2698c2ecf20Sopenharmony_ci DIV_FROM_REG(data->fan_div[nr]))); 2708c2ecf20Sopenharmony_ci} 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic ssize_t fan_div_show(struct device *dev, struct device_attribute *attr, 2738c2ecf20Sopenharmony_ci char *buf) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr(attr)->index; 2768c2ecf20Sopenharmony_ci struct gl518_data *data = gl518_update_device(dev); 2778c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci#define set(type, suffix, value, reg) \ 2818c2ecf20Sopenharmony_cistatic ssize_t set_##suffix(struct device *dev, \ 2828c2ecf20Sopenharmony_ci struct device_attribute *attr, \ 2838c2ecf20Sopenharmony_ci const char *buf, size_t count) \ 2848c2ecf20Sopenharmony_ci{ \ 2858c2ecf20Sopenharmony_ci struct gl518_data *data = dev_get_drvdata(dev); \ 2868c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; \ 2878c2ecf20Sopenharmony_ci long val; \ 2888c2ecf20Sopenharmony_ci int err = kstrtol(buf, 10, &val); \ 2898c2ecf20Sopenharmony_ci if (err) \ 2908c2ecf20Sopenharmony_ci return err; \ 2918c2ecf20Sopenharmony_ci \ 2928c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); \ 2938c2ecf20Sopenharmony_ci data->value = type##_TO_REG(val); \ 2948c2ecf20Sopenharmony_ci gl518_write_value(client, reg, data->value); \ 2958c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); \ 2968c2ecf20Sopenharmony_ci return count; \ 2978c2ecf20Sopenharmony_ci} 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci#define set_bits(type, suffix, value, reg, mask, shift) \ 3008c2ecf20Sopenharmony_cistatic ssize_t set_##suffix(struct device *dev, \ 3018c2ecf20Sopenharmony_ci struct device_attribute *attr, \ 3028c2ecf20Sopenharmony_ci const char *buf, size_t count) \ 3038c2ecf20Sopenharmony_ci{ \ 3048c2ecf20Sopenharmony_ci struct gl518_data *data = dev_get_drvdata(dev); \ 3058c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; \ 3068c2ecf20Sopenharmony_ci int regvalue; \ 3078c2ecf20Sopenharmony_ci unsigned long val; \ 3088c2ecf20Sopenharmony_ci int err = kstrtoul(buf, 10, &val); \ 3098c2ecf20Sopenharmony_ci if (err) \ 3108c2ecf20Sopenharmony_ci return err; \ 3118c2ecf20Sopenharmony_ci \ 3128c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); \ 3138c2ecf20Sopenharmony_ci regvalue = gl518_read_value(client, reg); \ 3148c2ecf20Sopenharmony_ci data->value = type##_TO_REG(val); \ 3158c2ecf20Sopenharmony_ci regvalue = (regvalue & ~mask) | (data->value << shift); \ 3168c2ecf20Sopenharmony_ci gl518_write_value(client, reg, regvalue); \ 3178c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); \ 3188c2ecf20Sopenharmony_ci return count; \ 3198c2ecf20Sopenharmony_ci} 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci#define set_low(type, suffix, value, reg) \ 3228c2ecf20Sopenharmony_ci set_bits(type, suffix, value, reg, 0x00ff, 0) 3238c2ecf20Sopenharmony_ci#define set_high(type, suffix, value, reg) \ 3248c2ecf20Sopenharmony_ci set_bits(type, suffix, value, reg, 0xff00, 8) 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ciset(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX); 3278c2ecf20Sopenharmony_ciset(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST); 3288c2ecf20Sopenharmony_ciset_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3); 3298c2ecf20Sopenharmony_ciset_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT); 3308c2ecf20Sopenharmony_ciset_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT); 3318c2ecf20Sopenharmony_ciset_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT); 3328c2ecf20Sopenharmony_ciset_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT); 3338c2ecf20Sopenharmony_ciset_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT); 3348c2ecf20Sopenharmony_ciset_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT); 3358c2ecf20Sopenharmony_ciset_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT); 3368c2ecf20Sopenharmony_ciset_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT); 3378c2ecf20Sopenharmony_ciset_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2); 3388c2ecf20Sopenharmony_ciset(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev, 3418c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 3428c2ecf20Sopenharmony_ci size_t count) 3438c2ecf20Sopenharmony_ci{ 3448c2ecf20Sopenharmony_ci struct gl518_data *data = dev_get_drvdata(dev); 3458c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 3468c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr(attr)->index; 3478c2ecf20Sopenharmony_ci int regvalue; 3488c2ecf20Sopenharmony_ci unsigned long val; 3498c2ecf20Sopenharmony_ci int err; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 3528c2ecf20Sopenharmony_ci if (err) 3538c2ecf20Sopenharmony_ci return err; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 3568c2ecf20Sopenharmony_ci regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); 3578c2ecf20Sopenharmony_ci data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); 3588c2ecf20Sopenharmony_ci regvalue = (regvalue & (0xff << (8 * nr))) 3598c2ecf20Sopenharmony_ci | (data->fan_min[nr] << (8 * (1 - nr))); 3608c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); 3638c2ecf20Sopenharmony_ci if (data->fan_min[nr] == 0) 3648c2ecf20Sopenharmony_ci data->alarm_mask &= ~(0x20 << nr); 3658c2ecf20Sopenharmony_ci else 3668c2ecf20Sopenharmony_ci data->alarm_mask |= (0x20 << nr); 3678c2ecf20Sopenharmony_ci data->beep_mask &= data->alarm_mask; 3688c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 3718c2ecf20Sopenharmony_ci return count; 3728c2ecf20Sopenharmony_ci} 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cistatic ssize_t fan_div_store(struct device *dev, 3758c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 3768c2ecf20Sopenharmony_ci size_t count) 3778c2ecf20Sopenharmony_ci{ 3788c2ecf20Sopenharmony_ci struct gl518_data *data = dev_get_drvdata(dev); 3798c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 3808c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr(attr)->index; 3818c2ecf20Sopenharmony_ci int regvalue; 3828c2ecf20Sopenharmony_ci unsigned long val; 3838c2ecf20Sopenharmony_ci int err; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 3868c2ecf20Sopenharmony_ci if (err) 3878c2ecf20Sopenharmony_ci return err; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci switch (val) { 3908c2ecf20Sopenharmony_ci case 1: 3918c2ecf20Sopenharmony_ci val = 0; 3928c2ecf20Sopenharmony_ci break; 3938c2ecf20Sopenharmony_ci case 2: 3948c2ecf20Sopenharmony_ci val = 1; 3958c2ecf20Sopenharmony_ci break; 3968c2ecf20Sopenharmony_ci case 4: 3978c2ecf20Sopenharmony_ci val = 2; 3988c2ecf20Sopenharmony_ci break; 3998c2ecf20Sopenharmony_ci case 8: 4008c2ecf20Sopenharmony_ci val = 3; 4018c2ecf20Sopenharmony_ci break; 4028c2ecf20Sopenharmony_ci default: 4038c2ecf20Sopenharmony_ci dev_err(dev, 4048c2ecf20Sopenharmony_ci "Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n", 4058c2ecf20Sopenharmony_ci val); 4068c2ecf20Sopenharmony_ci return -EINVAL; 4078c2ecf20Sopenharmony_ci } 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4108c2ecf20Sopenharmony_ci regvalue = gl518_read_value(client, GL518_REG_MISC); 4118c2ecf20Sopenharmony_ci data->fan_div[nr] = val; 4128c2ecf20Sopenharmony_ci regvalue = (regvalue & ~(0xc0 >> (2 * nr))) 4138c2ecf20Sopenharmony_ci | (data->fan_div[nr] << (6 - 2 * nr)); 4148c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_MISC, regvalue); 4158c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4168c2ecf20Sopenharmony_ci return count; 4178c2ecf20Sopenharmony_ci} 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temp1_input, 0444, show_temp_input1, NULL); 4208c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temp1_max, 0644, show_temp_max1, set_temp_max1); 4218c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temp1_max_hyst, 0644, 4228c2ecf20Sopenharmony_ci show_temp_hyst1, set_temp_hyst1); 4238c2ecf20Sopenharmony_cistatic DEVICE_ATTR(fan1_auto, 0644, show_fan_auto1, set_fan_auto1); 4248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); 4258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); 4268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); 4278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); 4288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0); 4298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1); 4308c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in0_input, 0444, show_in_input0, NULL); 4318c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in1_input, 0444, show_in_input1, NULL); 4328c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in2_input, 0444, show_in_input2, NULL); 4338c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in3_input, 0444, show_in_input3, NULL); 4348c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in0_min, 0644, show_in_min0, set_in_min0); 4358c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in1_min, 0644, show_in_min1, set_in_min1); 4368c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in2_min, 0644, show_in_min2, set_in_min2); 4378c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in3_min, 0644, show_in_min3, set_in_min3); 4388c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in0_max, 0644, show_in_max0, set_in_max0); 4398c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in1_max, 0644, show_in_max1, set_in_max1); 4408c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in2_max, 0644, show_in_max2, set_in_max2); 4418c2ecf20Sopenharmony_cistatic DEVICE_ATTR(in3_max, 0644, show_in_max3, set_in_max3); 4428c2ecf20Sopenharmony_cistatic DEVICE_ATTR(alarms, 0444, show_alarms, NULL); 4438c2ecf20Sopenharmony_cistatic DEVICE_ATTR(beep_enable, 0644, 4448c2ecf20Sopenharmony_ci show_beep_enable, set_beep_enable); 4458c2ecf20Sopenharmony_cistatic DEVICE_ATTR(beep_mask, 0644, 4468c2ecf20Sopenharmony_ci show_beep_mask, set_beep_mask); 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr, 4498c2ecf20Sopenharmony_ci char *buf) 4508c2ecf20Sopenharmony_ci{ 4518c2ecf20Sopenharmony_ci int bitnr = to_sensor_dev_attr(attr)->index; 4528c2ecf20Sopenharmony_ci struct gl518_data *data = gl518_update_device(dev); 4538c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); 4548c2ecf20Sopenharmony_ci} 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); 4578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); 4588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); 4598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); 4608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4); 4618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5); 4628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic ssize_t beep_show(struct device *dev, struct device_attribute *attr, 4658c2ecf20Sopenharmony_ci char *buf) 4668c2ecf20Sopenharmony_ci{ 4678c2ecf20Sopenharmony_ci int bitnr = to_sensor_dev_attr(attr)->index; 4688c2ecf20Sopenharmony_ci struct gl518_data *data = gl518_update_device(dev); 4698c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1); 4708c2ecf20Sopenharmony_ci} 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_cistatic ssize_t beep_store(struct device *dev, struct device_attribute *attr, 4738c2ecf20Sopenharmony_ci const char *buf, size_t count) 4748c2ecf20Sopenharmony_ci{ 4758c2ecf20Sopenharmony_ci struct gl518_data *data = dev_get_drvdata(dev); 4768c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 4778c2ecf20Sopenharmony_ci int bitnr = to_sensor_dev_attr(attr)->index; 4788c2ecf20Sopenharmony_ci unsigned long bit; 4798c2ecf20Sopenharmony_ci int err; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &bit); 4828c2ecf20Sopenharmony_ci if (err) 4838c2ecf20Sopenharmony_ci return err; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci if (bit & ~1) 4868c2ecf20Sopenharmony_ci return -EINVAL; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4898c2ecf20Sopenharmony_ci data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); 4908c2ecf20Sopenharmony_ci if (bit) 4918c2ecf20Sopenharmony_ci data->beep_mask |= (1 << bitnr); 4928c2ecf20Sopenharmony_ci else 4938c2ecf20Sopenharmony_ci data->beep_mask &= ~(1 << bitnr); 4948c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); 4958c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4968c2ecf20Sopenharmony_ci return count; 4978c2ecf20Sopenharmony_ci} 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0); 5008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1); 5018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2); 5028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3); 5038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4); 5048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5); 5058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6); 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_cistatic struct attribute *gl518_attributes[] = { 5088c2ecf20Sopenharmony_ci &dev_attr_in3_input.attr, 5098c2ecf20Sopenharmony_ci &dev_attr_in0_min.attr, 5108c2ecf20Sopenharmony_ci &dev_attr_in1_min.attr, 5118c2ecf20Sopenharmony_ci &dev_attr_in2_min.attr, 5128c2ecf20Sopenharmony_ci &dev_attr_in3_min.attr, 5138c2ecf20Sopenharmony_ci &dev_attr_in0_max.attr, 5148c2ecf20Sopenharmony_ci &dev_attr_in1_max.attr, 5158c2ecf20Sopenharmony_ci &dev_attr_in2_max.attr, 5168c2ecf20Sopenharmony_ci &dev_attr_in3_max.attr, 5178c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_alarm.dev_attr.attr, 5188c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_alarm.dev_attr.attr, 5198c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_alarm.dev_attr.attr, 5208c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_alarm.dev_attr.attr, 5218c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_beep.dev_attr.attr, 5228c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_beep.dev_attr.attr, 5238c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_beep.dev_attr.attr, 5248c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_beep.dev_attr.attr, 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci &dev_attr_fan1_auto.attr, 5278c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_input.dev_attr.attr, 5288c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_input.dev_attr.attr, 5298c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_min.dev_attr.attr, 5308c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_min.dev_attr.attr, 5318c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_div.dev_attr.attr, 5328c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_div.dev_attr.attr, 5338c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_alarm.dev_attr.attr, 5348c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_alarm.dev_attr.attr, 5358c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_beep.dev_attr.attr, 5368c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_beep.dev_attr.attr, 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci &dev_attr_temp1_input.attr, 5398c2ecf20Sopenharmony_ci &dev_attr_temp1_max.attr, 5408c2ecf20Sopenharmony_ci &dev_attr_temp1_max_hyst.attr, 5418c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_alarm.dev_attr.attr, 5428c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_beep.dev_attr.attr, 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 5458c2ecf20Sopenharmony_ci &dev_attr_beep_enable.attr, 5468c2ecf20Sopenharmony_ci &dev_attr_beep_mask.attr, 5478c2ecf20Sopenharmony_ci NULL 5488c2ecf20Sopenharmony_ci}; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_cistatic const struct attribute_group gl518_group = { 5518c2ecf20Sopenharmony_ci .attrs = gl518_attributes, 5528c2ecf20Sopenharmony_ci}; 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_cistatic struct attribute *gl518_attributes_r80[] = { 5558c2ecf20Sopenharmony_ci &dev_attr_in0_input.attr, 5568c2ecf20Sopenharmony_ci &dev_attr_in1_input.attr, 5578c2ecf20Sopenharmony_ci &dev_attr_in2_input.attr, 5588c2ecf20Sopenharmony_ci NULL 5598c2ecf20Sopenharmony_ci}; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_cistatic const struct attribute_group gl518_group_r80 = { 5628c2ecf20Sopenharmony_ci .attrs = gl518_attributes_r80, 5638c2ecf20Sopenharmony_ci}; 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci/* 5668c2ecf20Sopenharmony_ci * Real code 5678c2ecf20Sopenharmony_ci */ 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */ 5708c2ecf20Sopenharmony_cistatic int gl518_detect(struct i2c_client *client, struct i2c_board_info *info) 5718c2ecf20Sopenharmony_ci{ 5728c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 5738c2ecf20Sopenharmony_ci int rev; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 5768c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_WORD_DATA)) 5778c2ecf20Sopenharmony_ci return -ENODEV; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci /* Now, we do the remaining detection. */ 5808c2ecf20Sopenharmony_ci if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80) 5818c2ecf20Sopenharmony_ci || (gl518_read_value(client, GL518_REG_CONF) & 0x80)) 5828c2ecf20Sopenharmony_ci return -ENODEV; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci /* Determine the chip type. */ 5858c2ecf20Sopenharmony_ci rev = gl518_read_value(client, GL518_REG_REVISION); 5868c2ecf20Sopenharmony_ci if (rev != 0x00 && rev != 0x80) 5878c2ecf20Sopenharmony_ci return -ENODEV; 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_ci strlcpy(info->type, "gl518sm", I2C_NAME_SIZE); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci return 0; 5928c2ecf20Sopenharmony_ci} 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci/* 5958c2ecf20Sopenharmony_ci * Called when we have found a new GL518SM. 5968c2ecf20Sopenharmony_ci * Note that we preserve D4:NoFan2 and D2:beep_enable. 5978c2ecf20Sopenharmony_ci */ 5988c2ecf20Sopenharmony_cistatic void gl518_init_client(struct i2c_client *client) 5998c2ecf20Sopenharmony_ci{ 6008c2ecf20Sopenharmony_ci /* Make sure we leave D7:Reset untouched */ 6018c2ecf20Sopenharmony_ci u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f; 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci /* Comparator mode (D3=0), standby mode (D6=0) */ 6048c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37)); 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci /* Never interrupts */ 6078c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_MASK, 0x00); 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* Clear status register (D5=1), start (D6=1) */ 6108c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue); 6118c2ecf20Sopenharmony_ci gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue); 6128c2ecf20Sopenharmony_ci} 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_cistatic int gl518_probe(struct i2c_client *client) 6158c2ecf20Sopenharmony_ci{ 6168c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 6178c2ecf20Sopenharmony_ci struct device *hwmon_dev; 6188c2ecf20Sopenharmony_ci struct gl518_data *data; 6198c2ecf20Sopenharmony_ci int revision; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(struct gl518_data), GFP_KERNEL); 6228c2ecf20Sopenharmony_ci if (!data) 6238c2ecf20Sopenharmony_ci return -ENOMEM; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci data->client = client; 6268c2ecf20Sopenharmony_ci revision = gl518_read_value(client, GL518_REG_REVISION); 6278c2ecf20Sopenharmony_ci data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00; 6288c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci /* Initialize the GL518SM chip */ 6318c2ecf20Sopenharmony_ci data->alarm_mask = 0xff; 6328c2ecf20Sopenharmony_ci gl518_init_client(client); 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci /* sysfs hooks */ 6358c2ecf20Sopenharmony_ci data->groups[0] = &gl518_group; 6368c2ecf20Sopenharmony_ci if (data->type == gl518sm_r80) 6378c2ecf20Sopenharmony_ci data->groups[1] = &gl518_group_r80; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 6408c2ecf20Sopenharmony_ci data, data->groups); 6418c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 6428c2ecf20Sopenharmony_ci} 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_cistatic const struct i2c_device_id gl518_id[] = { 6458c2ecf20Sopenharmony_ci { "gl518sm", 0 }, 6468c2ecf20Sopenharmony_ci { } 6478c2ecf20Sopenharmony_ci}; 6488c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, gl518_id); 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_cistatic struct i2c_driver gl518_driver = { 6518c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 6528c2ecf20Sopenharmony_ci .driver = { 6538c2ecf20Sopenharmony_ci .name = "gl518sm", 6548c2ecf20Sopenharmony_ci }, 6558c2ecf20Sopenharmony_ci .probe_new = gl518_probe, 6568c2ecf20Sopenharmony_ci .id_table = gl518_id, 6578c2ecf20Sopenharmony_ci .detect = gl518_detect, 6588c2ecf20Sopenharmony_ci .address_list = normal_i2c, 6598c2ecf20Sopenharmony_ci}; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_cimodule_i2c_driver(gl518_driver); 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " 6648c2ecf20Sopenharmony_ci "Kyosti Malkki <kmalkki@cc.hut.fi> and " 6658c2ecf20Sopenharmony_ci "Hong-Gunn Chew <hglinux@gunnet.org>"); 6668c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GL518SM driver"); 6678c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 668