18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * w83791d.c - Part of lm_sensors, Linux kernel modules for hardware 48c2ecf20Sopenharmony_ci * monitoring 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2006-2007 Charles Spirakis <bezaur@gmail.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci/* 108c2ecf20Sopenharmony_ci * Supports following chips: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA 138c2ecf20Sopenharmony_ci * w83791d 10 5 5 3 0x71 0x5ca3 yes no 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * The w83791d chip appears to be part way between the 83781d and the 168c2ecf20Sopenharmony_ci * 83792d. Thus, this file is derived from both the w83792d.c and 178c2ecf20Sopenharmony_ci * w83781d.c files. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * The w83791g chip is the same as the w83791d but lead-free. 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/i2c.h> 268c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 278c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h> 288c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 298c2ecf20Sopenharmony_ci#include <linux/err.h> 308c2ecf20Sopenharmony_ci#include <linux/mutex.h> 318c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define NUMBER_OF_VIN 10 348c2ecf20Sopenharmony_ci#define NUMBER_OF_FANIN 5 358c2ecf20Sopenharmony_ci#define NUMBER_OF_TEMPIN 3 368c2ecf20Sopenharmony_ci#define NUMBER_OF_PWM 5 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* Addresses to scan */ 398c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, 408c2ecf20Sopenharmony_ci I2C_CLIENT_END }; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/* Insmod parameters */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic unsigned short force_subclients[4]; 458c2ecf20Sopenharmony_cimodule_param_array(force_subclients, short, NULL, 0); 468c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_subclients, 478c2ecf20Sopenharmony_ci "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}"); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic bool reset; 508c2ecf20Sopenharmony_cimodule_param(reset, bool, 0); 518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(reset, "Set to one to force a hardware chip reset"); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic bool init; 548c2ecf20Sopenharmony_cimodule_param(init, bool, 0); 558c2ecf20Sopenharmony_ciMODULE_PARM_DESC(init, "Set to one to force extra software initialization"); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* The W83791D registers */ 588c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_IN[NUMBER_OF_VIN] = { 598c2ecf20Sopenharmony_ci 0x20, /* VCOREA in DataSheet */ 608c2ecf20Sopenharmony_ci 0x21, /* VINR0 in DataSheet */ 618c2ecf20Sopenharmony_ci 0x22, /* +3.3VIN in DataSheet */ 628c2ecf20Sopenharmony_ci 0x23, /* VDD5V in DataSheet */ 638c2ecf20Sopenharmony_ci 0x24, /* +12VIN in DataSheet */ 648c2ecf20Sopenharmony_ci 0x25, /* -12VIN in DataSheet */ 658c2ecf20Sopenharmony_ci 0x26, /* -5VIN in DataSheet */ 668c2ecf20Sopenharmony_ci 0xB0, /* 5VSB in DataSheet */ 678c2ecf20Sopenharmony_ci 0xB1, /* VBAT in DataSheet */ 688c2ecf20Sopenharmony_ci 0xB2 /* VINR1 in DataSheet */ 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_IN_MAX[NUMBER_OF_VIN] = { 728c2ecf20Sopenharmony_ci 0x2B, /* VCOREA High Limit in DataSheet */ 738c2ecf20Sopenharmony_ci 0x2D, /* VINR0 High Limit in DataSheet */ 748c2ecf20Sopenharmony_ci 0x2F, /* +3.3VIN High Limit in DataSheet */ 758c2ecf20Sopenharmony_ci 0x31, /* VDD5V High Limit in DataSheet */ 768c2ecf20Sopenharmony_ci 0x33, /* +12VIN High Limit in DataSheet */ 778c2ecf20Sopenharmony_ci 0x35, /* -12VIN High Limit in DataSheet */ 788c2ecf20Sopenharmony_ci 0x37, /* -5VIN High Limit in DataSheet */ 798c2ecf20Sopenharmony_ci 0xB4, /* 5VSB High Limit in DataSheet */ 808c2ecf20Sopenharmony_ci 0xB6, /* VBAT High Limit in DataSheet */ 818c2ecf20Sopenharmony_ci 0xB8 /* VINR1 High Limit in DataSheet */ 828c2ecf20Sopenharmony_ci}; 838c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_IN_MIN[NUMBER_OF_VIN] = { 848c2ecf20Sopenharmony_ci 0x2C, /* VCOREA Low Limit in DataSheet */ 858c2ecf20Sopenharmony_ci 0x2E, /* VINR0 Low Limit in DataSheet */ 868c2ecf20Sopenharmony_ci 0x30, /* +3.3VIN Low Limit in DataSheet */ 878c2ecf20Sopenharmony_ci 0x32, /* VDD5V Low Limit in DataSheet */ 888c2ecf20Sopenharmony_ci 0x34, /* +12VIN Low Limit in DataSheet */ 898c2ecf20Sopenharmony_ci 0x36, /* -12VIN Low Limit in DataSheet */ 908c2ecf20Sopenharmony_ci 0x38, /* -5VIN Low Limit in DataSheet */ 918c2ecf20Sopenharmony_ci 0xB5, /* 5VSB Low Limit in DataSheet */ 928c2ecf20Sopenharmony_ci 0xB7, /* VBAT Low Limit in DataSheet */ 938c2ecf20Sopenharmony_ci 0xB9 /* VINR1 Low Limit in DataSheet */ 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_FAN[NUMBER_OF_FANIN] = { 968c2ecf20Sopenharmony_ci 0x28, /* FAN 1 Count in DataSheet */ 978c2ecf20Sopenharmony_ci 0x29, /* FAN 2 Count in DataSheet */ 988c2ecf20Sopenharmony_ci 0x2A, /* FAN 3 Count in DataSheet */ 998c2ecf20Sopenharmony_ci 0xBA, /* FAN 4 Count in DataSheet */ 1008c2ecf20Sopenharmony_ci 0xBB, /* FAN 5 Count in DataSheet */ 1018c2ecf20Sopenharmony_ci}; 1028c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_FAN_MIN[NUMBER_OF_FANIN] = { 1038c2ecf20Sopenharmony_ci 0x3B, /* FAN 1 Count Low Limit in DataSheet */ 1048c2ecf20Sopenharmony_ci 0x3C, /* FAN 2 Count Low Limit in DataSheet */ 1058c2ecf20Sopenharmony_ci 0x3D, /* FAN 3 Count Low Limit in DataSheet */ 1068c2ecf20Sopenharmony_ci 0xBC, /* FAN 4 Count Low Limit in DataSheet */ 1078c2ecf20Sopenharmony_ci 0xBD, /* FAN 5 Count Low Limit in DataSheet */ 1088c2ecf20Sopenharmony_ci}; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_PWM[NUMBER_OF_PWM] = { 1118c2ecf20Sopenharmony_ci 0x81, /* PWM 1 duty cycle register in DataSheet */ 1128c2ecf20Sopenharmony_ci 0x83, /* PWM 2 duty cycle register in DataSheet */ 1138c2ecf20Sopenharmony_ci 0x94, /* PWM 3 duty cycle register in DataSheet */ 1148c2ecf20Sopenharmony_ci 0xA0, /* PWM 4 duty cycle register in DataSheet */ 1158c2ecf20Sopenharmony_ci 0xA1, /* PWM 5 duty cycle register in DataSheet */ 1168c2ecf20Sopenharmony_ci}; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_TEMP_TARGET[3] = { 1198c2ecf20Sopenharmony_ci 0x85, /* PWM 1 target temperature for temp 1 */ 1208c2ecf20Sopenharmony_ci 0x86, /* PWM 2 target temperature for temp 2 */ 1218c2ecf20Sopenharmony_ci 0x96, /* PWM 3 target temperature for temp 3 */ 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_TEMP_TOL[2] = { 1258c2ecf20Sopenharmony_ci 0x87, /* PWM 1/2 temperature tolerance */ 1268c2ecf20Sopenharmony_ci 0x97, /* PWM 3 temperature tolerance */ 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_FAN_CFG[2] = { 1308c2ecf20Sopenharmony_ci 0x84, /* FAN 1/2 configuration */ 1318c2ecf20Sopenharmony_ci 0x95, /* FAN 3 configuration */ 1328c2ecf20Sopenharmony_ci}; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_FAN_DIV[3] = { 1358c2ecf20Sopenharmony_ci 0x47, /* contains FAN1 and FAN2 Divisor */ 1368c2ecf20Sopenharmony_ci 0x4b, /* contains FAN3 Divisor */ 1378c2ecf20Sopenharmony_ci 0x5C, /* contains FAN4 and FAN5 Divisor */ 1388c2ecf20Sopenharmony_ci}; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci#define W83791D_REG_BANK 0x4E 1418c2ecf20Sopenharmony_ci#define W83791D_REG_TEMP2_CONFIG 0xC2 1428c2ecf20Sopenharmony_ci#define W83791D_REG_TEMP3_CONFIG 0xCA 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_TEMP1[3] = { 1458c2ecf20Sopenharmony_ci 0x27, /* TEMP 1 in DataSheet */ 1468c2ecf20Sopenharmony_ci 0x39, /* TEMP 1 Over in DataSheet */ 1478c2ecf20Sopenharmony_ci 0x3A, /* TEMP 1 Hyst in DataSheet */ 1488c2ecf20Sopenharmony_ci}; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_TEMP_ADD[2][6] = { 1518c2ecf20Sopenharmony_ci {0xC0, /* TEMP 2 in DataSheet */ 1528c2ecf20Sopenharmony_ci 0xC1, /* TEMP 2(0.5 deg) in DataSheet */ 1538c2ecf20Sopenharmony_ci 0xC5, /* TEMP 2 Over High part in DataSheet */ 1548c2ecf20Sopenharmony_ci 0xC6, /* TEMP 2 Over Low part in DataSheet */ 1558c2ecf20Sopenharmony_ci 0xC3, /* TEMP 2 Thyst High part in DataSheet */ 1568c2ecf20Sopenharmony_ci 0xC4}, /* TEMP 2 Thyst Low part in DataSheet */ 1578c2ecf20Sopenharmony_ci {0xC8, /* TEMP 3 in DataSheet */ 1588c2ecf20Sopenharmony_ci 0xC9, /* TEMP 3(0.5 deg) in DataSheet */ 1598c2ecf20Sopenharmony_ci 0xCD, /* TEMP 3 Over High part in DataSheet */ 1608c2ecf20Sopenharmony_ci 0xCE, /* TEMP 3 Over Low part in DataSheet */ 1618c2ecf20Sopenharmony_ci 0xCB, /* TEMP 3 Thyst High part in DataSheet */ 1628c2ecf20Sopenharmony_ci 0xCC} /* TEMP 3 Thyst Low part in DataSheet */ 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci#define W83791D_REG_BEEP_CONFIG 0x4D 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic const u8 W83791D_REG_BEEP_CTRL[3] = { 1688c2ecf20Sopenharmony_ci 0x56, /* BEEP Control Register 1 */ 1698c2ecf20Sopenharmony_ci 0x57, /* BEEP Control Register 2 */ 1708c2ecf20Sopenharmony_ci 0xA3, /* BEEP Control Register 3 */ 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci#define W83791D_REG_GPIO 0x15 1748c2ecf20Sopenharmony_ci#define W83791D_REG_CONFIG 0x40 1758c2ecf20Sopenharmony_ci#define W83791D_REG_VID_FANDIV 0x47 1768c2ecf20Sopenharmony_ci#define W83791D_REG_DID_VID4 0x49 1778c2ecf20Sopenharmony_ci#define W83791D_REG_WCHIPID 0x58 1788c2ecf20Sopenharmony_ci#define W83791D_REG_CHIPMAN 0x4F 1798c2ecf20Sopenharmony_ci#define W83791D_REG_PIN 0x4B 1808c2ecf20Sopenharmony_ci#define W83791D_REG_I2C_SUBADDR 0x4A 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci#define W83791D_REG_ALARM1 0xA9 /* realtime status register1 */ 1838c2ecf20Sopenharmony_ci#define W83791D_REG_ALARM2 0xAA /* realtime status register2 */ 1848c2ecf20Sopenharmony_ci#define W83791D_REG_ALARM3 0xAB /* realtime status register3 */ 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci#define W83791D_REG_VBAT 0x5D 1878c2ecf20Sopenharmony_ci#define W83791D_REG_I2C_ADDR 0x48 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci/* 1908c2ecf20Sopenharmony_ci * The SMBus locks itself. The Winbond W83791D has a bank select register 1918c2ecf20Sopenharmony_ci * (index 0x4e), but the driver only accesses registers in bank 0. Since 1928c2ecf20Sopenharmony_ci * we don't switch banks, we don't need any special code to handle 1938c2ecf20Sopenharmony_ci * locking access between bank switches 1948c2ecf20Sopenharmony_ci */ 1958c2ecf20Sopenharmony_cistatic inline int w83791d_read(struct i2c_client *client, u8 reg) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci return i2c_smbus_read_byte_data(client, reg); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic inline int w83791d_write(struct i2c_client *client, u8 reg, u8 value) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci return i2c_smbus_write_byte_data(client, reg, value); 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci/* 2068c2ecf20Sopenharmony_ci * The analog voltage inputs have 16mV LSB. Since the sysfs output is 2078c2ecf20Sopenharmony_ci * in mV as would be measured on the chip input pin, need to just 2088c2ecf20Sopenharmony_ci * multiply/divide by 16 to translate from/to register values. 2098c2ecf20Sopenharmony_ci */ 2108c2ecf20Sopenharmony_ci#define IN_TO_REG(val) (clamp_val((((val) + 8) / 16), 0, 255)) 2118c2ecf20Sopenharmony_ci#define IN_FROM_REG(val) ((val) * 16) 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic u8 fan_to_reg(long rpm, int div) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci if (rpm == 0) 2168c2ecf20Sopenharmony_ci return 255; 2178c2ecf20Sopenharmony_ci rpm = clamp_val(rpm, 1, 1000000); 2188c2ecf20Sopenharmony_ci return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \ 2228c2ecf20Sopenharmony_ci ((val) == 255 ? 0 : \ 2238c2ecf20Sopenharmony_ci 1350000 / ((val) * (div)))) 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/* for temp1 which is 8-bit resolution, LSB = 1 degree Celsius */ 2268c2ecf20Sopenharmony_ci#define TEMP1_FROM_REG(val) ((val) * 1000) 2278c2ecf20Sopenharmony_ci#define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \ 2288c2ecf20Sopenharmony_ci (val) >= 127000 ? 127 : \ 2298c2ecf20Sopenharmony_ci (val) < 0 ? ((val) - 500) / 1000 : \ 2308c2ecf20Sopenharmony_ci ((val) + 500) / 1000) 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci/* 2338c2ecf20Sopenharmony_ci * for temp2 and temp3 which are 9-bit resolution, LSB = 0.5 degree Celsius 2348c2ecf20Sopenharmony_ci * Assumes the top 8 bits are the integral amount and the bottom 8 bits 2358c2ecf20Sopenharmony_ci * are the fractional amount. Since we only have 0.5 degree resolution, 2368c2ecf20Sopenharmony_ci * the bottom 7 bits will always be zero 2378c2ecf20Sopenharmony_ci */ 2388c2ecf20Sopenharmony_ci#define TEMP23_FROM_REG(val) ((val) / 128 * 500) 2398c2ecf20Sopenharmony_ci#define TEMP23_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), -128000, \ 2408c2ecf20Sopenharmony_ci 127500), 500) * 128) 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci/* for thermal cruise target temp, 7-bits, LSB = 1 degree Celsius */ 2438c2ecf20Sopenharmony_ci#define TARGET_TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 127000), \ 2448c2ecf20Sopenharmony_ci 1000) 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci/* for thermal cruise temp tolerance, 4-bits, LSB = 1 degree Celsius */ 2478c2ecf20Sopenharmony_ci#define TOL_TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 15000), \ 2488c2ecf20Sopenharmony_ci 1000) 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci#define BEEP_MASK_TO_REG(val) ((val) & 0xffffff) 2518c2ecf20Sopenharmony_ci#define BEEP_MASK_FROM_REG(val) ((val) & 0xffffff) 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val)) 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cistatic u8 div_to_reg(int nr, long val) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci int i; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* fan divisors max out at 128 */ 2608c2ecf20Sopenharmony_ci val = clamp_val(val, 1, 128) >> 1; 2618c2ecf20Sopenharmony_ci for (i = 0; i < 7; i++) { 2628c2ecf20Sopenharmony_ci if (val == 0) 2638c2ecf20Sopenharmony_ci break; 2648c2ecf20Sopenharmony_ci val >>= 1; 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci return (u8) i; 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistruct w83791d_data { 2708c2ecf20Sopenharmony_ci struct device *hwmon_dev; 2718c2ecf20Sopenharmony_ci struct mutex update_lock; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci char valid; /* !=0 if following fields are valid */ 2748c2ecf20Sopenharmony_ci unsigned long last_updated; /* In jiffies */ 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci /* volts */ 2778c2ecf20Sopenharmony_ci u8 in[NUMBER_OF_VIN]; /* Register value */ 2788c2ecf20Sopenharmony_ci u8 in_max[NUMBER_OF_VIN]; /* Register value */ 2798c2ecf20Sopenharmony_ci u8 in_min[NUMBER_OF_VIN]; /* Register value */ 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* fans */ 2828c2ecf20Sopenharmony_ci u8 fan[NUMBER_OF_FANIN]; /* Register value */ 2838c2ecf20Sopenharmony_ci u8 fan_min[NUMBER_OF_FANIN]; /* Register value */ 2848c2ecf20Sopenharmony_ci u8 fan_div[NUMBER_OF_FANIN]; /* Register encoding, shifted right */ 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci /* Temperature sensors */ 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci s8 temp1[3]; /* current, over, thyst */ 2898c2ecf20Sopenharmony_ci s16 temp_add[2][3]; /* fixed point value. Top 8 bits are the 2908c2ecf20Sopenharmony_ci * integral part, bottom 8 bits are the 2918c2ecf20Sopenharmony_ci * fractional part. We only use the top 2928c2ecf20Sopenharmony_ci * 9 bits as the resolution is only 2938c2ecf20Sopenharmony_ci * to the 0.5 degree C... 2948c2ecf20Sopenharmony_ci * two sensors with three values 2958c2ecf20Sopenharmony_ci * (cur, over, hyst) 2968c2ecf20Sopenharmony_ci */ 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci /* PWMs */ 2998c2ecf20Sopenharmony_ci u8 pwm[5]; /* pwm duty cycle */ 3008c2ecf20Sopenharmony_ci u8 pwm_enable[3]; /* pwm enable status for fan 1-3 3018c2ecf20Sopenharmony_ci * (fan 4-5 only support manual mode) 3028c2ecf20Sopenharmony_ci */ 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci u8 temp_target[3]; /* pwm 1-3 target temperature */ 3058c2ecf20Sopenharmony_ci u8 temp_tolerance[3]; /* pwm 1-3 temperature tolerance */ 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci /* Misc */ 3088c2ecf20Sopenharmony_ci u32 alarms; /* realtime status register encoding,combined */ 3098c2ecf20Sopenharmony_ci u8 beep_enable; /* Global beep enable */ 3108c2ecf20Sopenharmony_ci u32 beep_mask; /* Mask off specific beeps */ 3118c2ecf20Sopenharmony_ci u8 vid; /* Register encoding, combined */ 3128c2ecf20Sopenharmony_ci u8 vrm; /* hwmon-vid */ 3138c2ecf20Sopenharmony_ci}; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_cistatic int w83791d_probe(struct i2c_client *client); 3168c2ecf20Sopenharmony_cistatic int w83791d_detect(struct i2c_client *client, 3178c2ecf20Sopenharmony_ci struct i2c_board_info *info); 3188c2ecf20Sopenharmony_cistatic int w83791d_remove(struct i2c_client *client); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_cistatic int w83791d_read(struct i2c_client *client, u8 reg); 3218c2ecf20Sopenharmony_cistatic int w83791d_write(struct i2c_client *client, u8 reg, u8 value); 3228c2ecf20Sopenharmony_cistatic struct w83791d_data *w83791d_update_device(struct device *dev); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci#ifdef DEBUG 3258c2ecf20Sopenharmony_cistatic void w83791d_print_debug(struct w83791d_data *data, struct device *dev); 3268c2ecf20Sopenharmony_ci#endif 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_cistatic void w83791d_init_client(struct i2c_client *client); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cistatic const struct i2c_device_id w83791d_id[] = { 3318c2ecf20Sopenharmony_ci { "w83791d", 0 }, 3328c2ecf20Sopenharmony_ci { } 3338c2ecf20Sopenharmony_ci}; 3348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, w83791d_id); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_cistatic struct i2c_driver w83791d_driver = { 3378c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 3388c2ecf20Sopenharmony_ci .driver = { 3398c2ecf20Sopenharmony_ci .name = "w83791d", 3408c2ecf20Sopenharmony_ci }, 3418c2ecf20Sopenharmony_ci .probe_new = w83791d_probe, 3428c2ecf20Sopenharmony_ci .remove = w83791d_remove, 3438c2ecf20Sopenharmony_ci .id_table = w83791d_id, 3448c2ecf20Sopenharmony_ci .detect = w83791d_detect, 3458c2ecf20Sopenharmony_ci .address_list = normal_i2c, 3468c2ecf20Sopenharmony_ci}; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci/* following are the sysfs callback functions */ 3498c2ecf20Sopenharmony_ci#define show_in_reg(reg) \ 3508c2ecf20Sopenharmony_cistatic ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ 3518c2ecf20Sopenharmony_ci char *buf) \ 3528c2ecf20Sopenharmony_ci{ \ 3538c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = \ 3548c2ecf20Sopenharmony_ci to_sensor_dev_attr(attr); \ 3558c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); \ 3568c2ecf20Sopenharmony_ci int nr = sensor_attr->index; \ 3578c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \ 3588c2ecf20Sopenharmony_ci} 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_cishow_in_reg(in); 3618c2ecf20Sopenharmony_cishow_in_reg(in_min); 3628c2ecf20Sopenharmony_cishow_in_reg(in_max); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci#define store_in_reg(REG, reg) \ 3658c2ecf20Sopenharmony_cistatic ssize_t store_in_##reg(struct device *dev, \ 3668c2ecf20Sopenharmony_ci struct device_attribute *attr, \ 3678c2ecf20Sopenharmony_ci const char *buf, size_t count) \ 3688c2ecf20Sopenharmony_ci{ \ 3698c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = \ 3708c2ecf20Sopenharmony_ci to_sensor_dev_attr(attr); \ 3718c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); \ 3728c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); \ 3738c2ecf20Sopenharmony_ci int nr = sensor_attr->index; \ 3748c2ecf20Sopenharmony_ci unsigned long val; \ 3758c2ecf20Sopenharmony_ci int err = kstrtoul(buf, 10, &val); \ 3768c2ecf20Sopenharmony_ci if (err) \ 3778c2ecf20Sopenharmony_ci return err; \ 3788c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); \ 3798c2ecf20Sopenharmony_ci data->in_##reg[nr] = IN_TO_REG(val); \ 3808c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_IN_##REG[nr], data->in_##reg[nr]); \ 3818c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); \ 3828c2ecf20Sopenharmony_ci \ 3838c2ecf20Sopenharmony_ci return count; \ 3848c2ecf20Sopenharmony_ci} 3858c2ecf20Sopenharmony_cistore_in_reg(MIN, min); 3868c2ecf20Sopenharmony_cistore_in_reg(MAX, max); 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_in_input[] = { 3898c2ecf20Sopenharmony_ci SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0), 3908c2ecf20Sopenharmony_ci SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1), 3918c2ecf20Sopenharmony_ci SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2), 3928c2ecf20Sopenharmony_ci SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3), 3938c2ecf20Sopenharmony_ci SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4), 3948c2ecf20Sopenharmony_ci SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5), 3958c2ecf20Sopenharmony_ci SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6), 3968c2ecf20Sopenharmony_ci SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7), 3978c2ecf20Sopenharmony_ci SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8), 3988c2ecf20Sopenharmony_ci SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9), 3998c2ecf20Sopenharmony_ci}; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_in_min[] = { 4028c2ecf20Sopenharmony_ci SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0), 4038c2ecf20Sopenharmony_ci SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1), 4048c2ecf20Sopenharmony_ci SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2), 4058c2ecf20Sopenharmony_ci SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3), 4068c2ecf20Sopenharmony_ci SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4), 4078c2ecf20Sopenharmony_ci SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5), 4088c2ecf20Sopenharmony_ci SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6), 4098c2ecf20Sopenharmony_ci SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7), 4108c2ecf20Sopenharmony_ci SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8), 4118c2ecf20Sopenharmony_ci SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9), 4128c2ecf20Sopenharmony_ci}; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_in_max[] = { 4158c2ecf20Sopenharmony_ci SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0), 4168c2ecf20Sopenharmony_ci SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1), 4178c2ecf20Sopenharmony_ci SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2), 4188c2ecf20Sopenharmony_ci SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3), 4198c2ecf20Sopenharmony_ci SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4), 4208c2ecf20Sopenharmony_ci SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5), 4218c2ecf20Sopenharmony_ci SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6), 4228c2ecf20Sopenharmony_ci SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7), 4238c2ecf20Sopenharmony_ci SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8), 4248c2ecf20Sopenharmony_ci SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9), 4258c2ecf20Sopenharmony_ci}; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_cistatic ssize_t show_beep(struct device *dev, struct device_attribute *attr, 4298c2ecf20Sopenharmony_ci char *buf) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = 4328c2ecf20Sopenharmony_ci to_sensor_dev_attr(attr); 4338c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 4348c2ecf20Sopenharmony_ci int bitnr = sensor_attr->index; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1); 4378c2ecf20Sopenharmony_ci} 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cistatic ssize_t store_beep(struct device *dev, struct device_attribute *attr, 4408c2ecf20Sopenharmony_ci const char *buf, size_t count) 4418c2ecf20Sopenharmony_ci{ 4428c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = 4438c2ecf20Sopenharmony_ci to_sensor_dev_attr(attr); 4448c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 4458c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 4468c2ecf20Sopenharmony_ci int bitnr = sensor_attr->index; 4478c2ecf20Sopenharmony_ci int bytenr = bitnr / 8; 4488c2ecf20Sopenharmony_ci unsigned long val; 4498c2ecf20Sopenharmony_ci int err; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 4528c2ecf20Sopenharmony_ci if (err) 4538c2ecf20Sopenharmony_ci return err; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci val = val ? 1 : 0; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci data->beep_mask &= ~(0xff << (bytenr * 8)); 4608c2ecf20Sopenharmony_ci data->beep_mask |= w83791d_read(client, W83791D_REG_BEEP_CTRL[bytenr]) 4618c2ecf20Sopenharmony_ci << (bytenr * 8); 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci data->beep_mask &= ~(1 << bitnr); 4648c2ecf20Sopenharmony_ci data->beep_mask |= val << bitnr; 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_BEEP_CTRL[bytenr], 4678c2ecf20Sopenharmony_ci (data->beep_mask >> (bytenr * 8)) & 0xff); 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci return count; 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_cistatic ssize_t show_alarm(struct device *dev, struct device_attribute *attr, 4758c2ecf20Sopenharmony_ci char *buf) 4768c2ecf20Sopenharmony_ci{ 4778c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = 4788c2ecf20Sopenharmony_ci to_sensor_dev_attr(attr); 4798c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 4808c2ecf20Sopenharmony_ci int bitnr = sensor_attr->index; 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci/* 4868c2ecf20Sopenharmony_ci * Note: The bitmask for the beep enable/disable is different than 4878c2ecf20Sopenharmony_ci * the bitmask for the alarm. 4888c2ecf20Sopenharmony_ci */ 4898c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_in_beep[] = { 4908c2ecf20Sopenharmony_ci SENSOR_ATTR(in0_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 0), 4918c2ecf20Sopenharmony_ci SENSOR_ATTR(in1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 13), 4928c2ecf20Sopenharmony_ci SENSOR_ATTR(in2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 2), 4938c2ecf20Sopenharmony_ci SENSOR_ATTR(in3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 3), 4948c2ecf20Sopenharmony_ci SENSOR_ATTR(in4_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 8), 4958c2ecf20Sopenharmony_ci SENSOR_ATTR(in5_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 9), 4968c2ecf20Sopenharmony_ci SENSOR_ATTR(in6_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 10), 4978c2ecf20Sopenharmony_ci SENSOR_ATTR(in7_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 16), 4988c2ecf20Sopenharmony_ci SENSOR_ATTR(in8_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 17), 4998c2ecf20Sopenharmony_ci SENSOR_ATTR(in9_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 14), 5008c2ecf20Sopenharmony_ci}; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_in_alarm[] = { 5038c2ecf20Sopenharmony_ci SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0), 5048c2ecf20Sopenharmony_ci SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1), 5058c2ecf20Sopenharmony_ci SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2), 5068c2ecf20Sopenharmony_ci SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3), 5078c2ecf20Sopenharmony_ci SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8), 5088c2ecf20Sopenharmony_ci SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9), 5098c2ecf20Sopenharmony_ci SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10), 5108c2ecf20Sopenharmony_ci SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 19), 5118c2ecf20Sopenharmony_ci SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 20), 5128c2ecf20Sopenharmony_ci SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 14), 5138c2ecf20Sopenharmony_ci}; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci#define show_fan_reg(reg) \ 5168c2ecf20Sopenharmony_cistatic ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ 5178c2ecf20Sopenharmony_ci char *buf) \ 5188c2ecf20Sopenharmony_ci{ \ 5198c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = \ 5208c2ecf20Sopenharmony_ci to_sensor_dev_attr(attr); \ 5218c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); \ 5228c2ecf20Sopenharmony_ci int nr = sensor_attr->index; \ 5238c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", \ 5248c2ecf20Sopenharmony_ci FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \ 5258c2ecf20Sopenharmony_ci} 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_cishow_fan_reg(fan); 5288c2ecf20Sopenharmony_cishow_fan_reg(fan_min); 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_cistatic ssize_t store_fan_min(struct device *dev, struct device_attribute *attr, 5318c2ecf20Sopenharmony_ci const char *buf, size_t count) 5328c2ecf20Sopenharmony_ci{ 5338c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 5348c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 5358c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 5368c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 5378c2ecf20Sopenharmony_ci unsigned long val; 5388c2ecf20Sopenharmony_ci int err; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 5418c2ecf20Sopenharmony_ci if (err) 5428c2ecf20Sopenharmony_ci return err; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5458c2ecf20Sopenharmony_ci data->fan_min[nr] = fan_to_reg(val, DIV_FROM_REG(data->fan_div[nr])); 5468c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->fan_min[nr]); 5478c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci return count; 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_cistatic ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, 5538c2ecf20Sopenharmony_ci char *buf) 5548c2ecf20Sopenharmony_ci{ 5558c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 5568c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 5578c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 5588c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr])); 5598c2ecf20Sopenharmony_ci} 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci/* 5628c2ecf20Sopenharmony_ci * Note: we save and restore the fan minimum here, because its value is 5638c2ecf20Sopenharmony_ci * determined in part by the fan divisor. This follows the principle of 5648c2ecf20Sopenharmony_ci * least surprise; the user doesn't expect the fan minimum to change just 5658c2ecf20Sopenharmony_ci * because the divisor changed. 5668c2ecf20Sopenharmony_ci */ 5678c2ecf20Sopenharmony_cistatic ssize_t store_fan_div(struct device *dev, struct device_attribute *attr, 5688c2ecf20Sopenharmony_ci const char *buf, size_t count) 5698c2ecf20Sopenharmony_ci{ 5708c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 5718c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 5728c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 5738c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 5748c2ecf20Sopenharmony_ci unsigned long min; 5758c2ecf20Sopenharmony_ci u8 tmp_fan_div; 5768c2ecf20Sopenharmony_ci u8 fan_div_reg; 5778c2ecf20Sopenharmony_ci u8 vbat_reg; 5788c2ecf20Sopenharmony_ci int indx = 0; 5798c2ecf20Sopenharmony_ci u8 keep_mask = 0; 5808c2ecf20Sopenharmony_ci u8 new_shift = 0; 5818c2ecf20Sopenharmony_ci unsigned long val; 5828c2ecf20Sopenharmony_ci int err; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 5858c2ecf20Sopenharmony_ci if (err) 5868c2ecf20Sopenharmony_ci return err; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci /* Save fan_min */ 5898c2ecf20Sopenharmony_ci min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 5928c2ecf20Sopenharmony_ci data->fan_div[nr] = div_to_reg(nr, val); 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci switch (nr) { 5958c2ecf20Sopenharmony_ci case 0: 5968c2ecf20Sopenharmony_ci indx = 0; 5978c2ecf20Sopenharmony_ci keep_mask = 0xcf; 5988c2ecf20Sopenharmony_ci new_shift = 4; 5998c2ecf20Sopenharmony_ci break; 6008c2ecf20Sopenharmony_ci case 1: 6018c2ecf20Sopenharmony_ci indx = 0; 6028c2ecf20Sopenharmony_ci keep_mask = 0x3f; 6038c2ecf20Sopenharmony_ci new_shift = 6; 6048c2ecf20Sopenharmony_ci break; 6058c2ecf20Sopenharmony_ci case 2: 6068c2ecf20Sopenharmony_ci indx = 1; 6078c2ecf20Sopenharmony_ci keep_mask = 0x3f; 6088c2ecf20Sopenharmony_ci new_shift = 6; 6098c2ecf20Sopenharmony_ci break; 6108c2ecf20Sopenharmony_ci case 3: 6118c2ecf20Sopenharmony_ci indx = 2; 6128c2ecf20Sopenharmony_ci keep_mask = 0xf8; 6138c2ecf20Sopenharmony_ci new_shift = 0; 6148c2ecf20Sopenharmony_ci break; 6158c2ecf20Sopenharmony_ci case 4: 6168c2ecf20Sopenharmony_ci indx = 2; 6178c2ecf20Sopenharmony_ci keep_mask = 0x8f; 6188c2ecf20Sopenharmony_ci new_shift = 4; 6198c2ecf20Sopenharmony_ci break; 6208c2ecf20Sopenharmony_ci#ifdef DEBUG 6218c2ecf20Sopenharmony_ci default: 6228c2ecf20Sopenharmony_ci dev_warn(dev, "store_fan_div: Unexpected nr seen: %d\n", nr); 6238c2ecf20Sopenharmony_ci count = -EINVAL; 6248c2ecf20Sopenharmony_ci goto err_exit; 6258c2ecf20Sopenharmony_ci#endif 6268c2ecf20Sopenharmony_ci } 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci fan_div_reg = w83791d_read(client, W83791D_REG_FAN_DIV[indx]) 6298c2ecf20Sopenharmony_ci & keep_mask; 6308c2ecf20Sopenharmony_ci tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask; 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_FAN_DIV[indx], 6338c2ecf20Sopenharmony_ci fan_div_reg | tmp_fan_div); 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci /* Bit 2 of fans 0-2 is stored in the vbat register (bits 5-7) */ 6368c2ecf20Sopenharmony_ci if (nr < 3) { 6378c2ecf20Sopenharmony_ci keep_mask = ~(1 << (nr + 5)); 6388c2ecf20Sopenharmony_ci vbat_reg = w83791d_read(client, W83791D_REG_VBAT) 6398c2ecf20Sopenharmony_ci & keep_mask; 6408c2ecf20Sopenharmony_ci tmp_fan_div = (data->fan_div[nr] << (3 + nr)) & ~keep_mask; 6418c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_VBAT, 6428c2ecf20Sopenharmony_ci vbat_reg | tmp_fan_div); 6438c2ecf20Sopenharmony_ci } 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci /* Restore fan_min */ 6468c2ecf20Sopenharmony_ci data->fan_min[nr] = fan_to_reg(min, DIV_FROM_REG(data->fan_div[nr])); 6478c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->fan_min[nr]); 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci#ifdef DEBUG 6508c2ecf20Sopenharmony_cierr_exit: 6518c2ecf20Sopenharmony_ci#endif 6528c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci return count; 6558c2ecf20Sopenharmony_ci} 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_fan_input[] = { 6588c2ecf20Sopenharmony_ci SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0), 6598c2ecf20Sopenharmony_ci SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1), 6608c2ecf20Sopenharmony_ci SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2), 6618c2ecf20Sopenharmony_ci SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3), 6628c2ecf20Sopenharmony_ci SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4), 6638c2ecf20Sopenharmony_ci}; 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_fan_min[] = { 6668c2ecf20Sopenharmony_ci SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, 6678c2ecf20Sopenharmony_ci show_fan_min, store_fan_min, 0), 6688c2ecf20Sopenharmony_ci SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, 6698c2ecf20Sopenharmony_ci show_fan_min, store_fan_min, 1), 6708c2ecf20Sopenharmony_ci SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, 6718c2ecf20Sopenharmony_ci show_fan_min, store_fan_min, 2), 6728c2ecf20Sopenharmony_ci SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, 6738c2ecf20Sopenharmony_ci show_fan_min, store_fan_min, 3), 6748c2ecf20Sopenharmony_ci SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, 6758c2ecf20Sopenharmony_ci show_fan_min, store_fan_min, 4), 6768c2ecf20Sopenharmony_ci}; 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_fan_div[] = { 6798c2ecf20Sopenharmony_ci SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, 6808c2ecf20Sopenharmony_ci show_fan_div, store_fan_div, 0), 6818c2ecf20Sopenharmony_ci SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, 6828c2ecf20Sopenharmony_ci show_fan_div, store_fan_div, 1), 6838c2ecf20Sopenharmony_ci SENSOR_ATTR(fan3_div, S_IWUSR | S_IRUGO, 6848c2ecf20Sopenharmony_ci show_fan_div, store_fan_div, 2), 6858c2ecf20Sopenharmony_ci SENSOR_ATTR(fan4_div, S_IWUSR | S_IRUGO, 6868c2ecf20Sopenharmony_ci show_fan_div, store_fan_div, 3), 6878c2ecf20Sopenharmony_ci SENSOR_ATTR(fan5_div, S_IWUSR | S_IRUGO, 6888c2ecf20Sopenharmony_ci show_fan_div, store_fan_div, 4), 6898c2ecf20Sopenharmony_ci}; 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_fan_beep[] = { 6928c2ecf20Sopenharmony_ci SENSOR_ATTR(fan1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 6), 6938c2ecf20Sopenharmony_ci SENSOR_ATTR(fan2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 7), 6948c2ecf20Sopenharmony_ci SENSOR_ATTR(fan3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 11), 6958c2ecf20Sopenharmony_ci SENSOR_ATTR(fan4_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 21), 6968c2ecf20Sopenharmony_ci SENSOR_ATTR(fan5_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 22), 6978c2ecf20Sopenharmony_ci}; 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_fan_alarm[] = { 7008c2ecf20Sopenharmony_ci SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6), 7018c2ecf20Sopenharmony_ci SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7), 7028c2ecf20Sopenharmony_ci SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11), 7038c2ecf20Sopenharmony_ci SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 21), 7048c2ecf20Sopenharmony_ci SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 22), 7058c2ecf20Sopenharmony_ci}; 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci/* read/write PWMs */ 7088c2ecf20Sopenharmony_cistatic ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 7098c2ecf20Sopenharmony_ci char *buf) 7108c2ecf20Sopenharmony_ci{ 7118c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 7128c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 7138c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 7148c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", data->pwm[nr]); 7158c2ecf20Sopenharmony_ci} 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_cistatic ssize_t store_pwm(struct device *dev, struct device_attribute *attr, 7188c2ecf20Sopenharmony_ci const char *buf, size_t count) 7198c2ecf20Sopenharmony_ci{ 7208c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 7218c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 7228c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 7238c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 7248c2ecf20Sopenharmony_ci unsigned long val; 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci if (kstrtoul(buf, 10, &val)) 7278c2ecf20Sopenharmony_ci return -EINVAL; 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 7308c2ecf20Sopenharmony_ci data->pwm[nr] = clamp_val(val, 0, 255); 7318c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_PWM[nr], data->pwm[nr]); 7328c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 7338c2ecf20Sopenharmony_ci return count; 7348c2ecf20Sopenharmony_ci} 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_pwm[] = { 7378c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, 7388c2ecf20Sopenharmony_ci show_pwm, store_pwm, 0), 7398c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, 7408c2ecf20Sopenharmony_ci show_pwm, store_pwm, 1), 7418c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, 7428c2ecf20Sopenharmony_ci show_pwm, store_pwm, 2), 7438c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, 7448c2ecf20Sopenharmony_ci show_pwm, store_pwm, 3), 7458c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm5, S_IWUSR | S_IRUGO, 7468c2ecf20Sopenharmony_ci show_pwm, store_pwm, 4), 7478c2ecf20Sopenharmony_ci}; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_cistatic ssize_t show_pwmenable(struct device *dev, struct device_attribute *attr, 7508c2ecf20Sopenharmony_ci char *buf) 7518c2ecf20Sopenharmony_ci{ 7528c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 7538c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 7548c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 7558c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", data->pwm_enable[nr] + 1); 7568c2ecf20Sopenharmony_ci} 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_cistatic ssize_t store_pwmenable(struct device *dev, 7598c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t count) 7608c2ecf20Sopenharmony_ci{ 7618c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 7628c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 7638c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 7648c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 7658c2ecf20Sopenharmony_ci unsigned long val; 7668c2ecf20Sopenharmony_ci u8 reg_cfg_tmp; 7678c2ecf20Sopenharmony_ci u8 reg_idx = 0; 7688c2ecf20Sopenharmony_ci u8 val_shift = 0; 7698c2ecf20Sopenharmony_ci u8 keep_mask = 0; 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci int ret = kstrtoul(buf, 10, &val); 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci if (ret || val < 1 || val > 3) 7748c2ecf20Sopenharmony_ci return -EINVAL; 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 7778c2ecf20Sopenharmony_ci data->pwm_enable[nr] = val - 1; 7788c2ecf20Sopenharmony_ci switch (nr) { 7798c2ecf20Sopenharmony_ci case 0: 7808c2ecf20Sopenharmony_ci reg_idx = 0; 7818c2ecf20Sopenharmony_ci val_shift = 2; 7828c2ecf20Sopenharmony_ci keep_mask = 0xf3; 7838c2ecf20Sopenharmony_ci break; 7848c2ecf20Sopenharmony_ci case 1: 7858c2ecf20Sopenharmony_ci reg_idx = 0; 7868c2ecf20Sopenharmony_ci val_shift = 4; 7878c2ecf20Sopenharmony_ci keep_mask = 0xcf; 7888c2ecf20Sopenharmony_ci break; 7898c2ecf20Sopenharmony_ci case 2: 7908c2ecf20Sopenharmony_ci reg_idx = 1; 7918c2ecf20Sopenharmony_ci val_shift = 2; 7928c2ecf20Sopenharmony_ci keep_mask = 0xf3; 7938c2ecf20Sopenharmony_ci break; 7948c2ecf20Sopenharmony_ci } 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci reg_cfg_tmp = w83791d_read(client, W83791D_REG_FAN_CFG[reg_idx]); 7978c2ecf20Sopenharmony_ci reg_cfg_tmp = (reg_cfg_tmp & keep_mask) | 7988c2ecf20Sopenharmony_ci data->pwm_enable[nr] << val_shift; 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_FAN_CFG[reg_idx], reg_cfg_tmp); 8018c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci return count; 8048c2ecf20Sopenharmony_ci} 8058c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_pwmenable[] = { 8068c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, 8078c2ecf20Sopenharmony_ci show_pwmenable, store_pwmenable, 0), 8088c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, 8098c2ecf20Sopenharmony_ci show_pwmenable, store_pwmenable, 1), 8108c2ecf20Sopenharmony_ci SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, 8118c2ecf20Sopenharmony_ci show_pwmenable, store_pwmenable, 2), 8128c2ecf20Sopenharmony_ci}; 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci/* For Smart Fan I / Thermal Cruise */ 8158c2ecf20Sopenharmony_cistatic ssize_t show_temp_target(struct device *dev, 8168c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 8178c2ecf20Sopenharmony_ci{ 8188c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 8198c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 8208c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 8218c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp_target[nr])); 8228c2ecf20Sopenharmony_ci} 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_cistatic ssize_t store_temp_target(struct device *dev, 8258c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t count) 8268c2ecf20Sopenharmony_ci{ 8278c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 8288c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 8298c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 8308c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 8318c2ecf20Sopenharmony_ci long val; 8328c2ecf20Sopenharmony_ci u8 target_mask; 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci if (kstrtol(buf, 10, &val)) 8358c2ecf20Sopenharmony_ci return -EINVAL; 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 8388c2ecf20Sopenharmony_ci data->temp_target[nr] = TARGET_TEMP_TO_REG(val); 8398c2ecf20Sopenharmony_ci target_mask = w83791d_read(client, 8408c2ecf20Sopenharmony_ci W83791D_REG_TEMP_TARGET[nr]) & 0x80; 8418c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP_TARGET[nr], 8428c2ecf20Sopenharmony_ci data->temp_target[nr] | target_mask); 8438c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 8448c2ecf20Sopenharmony_ci return count; 8458c2ecf20Sopenharmony_ci} 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_temp_target[] = { 8488c2ecf20Sopenharmony_ci SENSOR_ATTR(temp1_target, S_IWUSR | S_IRUGO, 8498c2ecf20Sopenharmony_ci show_temp_target, store_temp_target, 0), 8508c2ecf20Sopenharmony_ci SENSOR_ATTR(temp2_target, S_IWUSR | S_IRUGO, 8518c2ecf20Sopenharmony_ci show_temp_target, store_temp_target, 1), 8528c2ecf20Sopenharmony_ci SENSOR_ATTR(temp3_target, S_IWUSR | S_IRUGO, 8538c2ecf20Sopenharmony_ci show_temp_target, store_temp_target, 2), 8548c2ecf20Sopenharmony_ci}; 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_cistatic ssize_t show_temp_tolerance(struct device *dev, 8578c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 8588c2ecf20Sopenharmony_ci{ 8598c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 8608c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 8618c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 8628c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp_tolerance[nr])); 8638c2ecf20Sopenharmony_ci} 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_cistatic ssize_t store_temp_tolerance(struct device *dev, 8668c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t count) 8678c2ecf20Sopenharmony_ci{ 8688c2ecf20Sopenharmony_ci struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 8698c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 8708c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 8718c2ecf20Sopenharmony_ci int nr = sensor_attr->index; 8728c2ecf20Sopenharmony_ci unsigned long val; 8738c2ecf20Sopenharmony_ci u8 target_mask; 8748c2ecf20Sopenharmony_ci u8 reg_idx = 0; 8758c2ecf20Sopenharmony_ci u8 val_shift = 0; 8768c2ecf20Sopenharmony_ci u8 keep_mask = 0; 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci if (kstrtoul(buf, 10, &val)) 8798c2ecf20Sopenharmony_ci return -EINVAL; 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_ci switch (nr) { 8828c2ecf20Sopenharmony_ci case 0: 8838c2ecf20Sopenharmony_ci reg_idx = 0; 8848c2ecf20Sopenharmony_ci val_shift = 0; 8858c2ecf20Sopenharmony_ci keep_mask = 0xf0; 8868c2ecf20Sopenharmony_ci break; 8878c2ecf20Sopenharmony_ci case 1: 8888c2ecf20Sopenharmony_ci reg_idx = 0; 8898c2ecf20Sopenharmony_ci val_shift = 4; 8908c2ecf20Sopenharmony_ci keep_mask = 0x0f; 8918c2ecf20Sopenharmony_ci break; 8928c2ecf20Sopenharmony_ci case 2: 8938c2ecf20Sopenharmony_ci reg_idx = 1; 8948c2ecf20Sopenharmony_ci val_shift = 0; 8958c2ecf20Sopenharmony_ci keep_mask = 0xf0; 8968c2ecf20Sopenharmony_ci break; 8978c2ecf20Sopenharmony_ci } 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 9008c2ecf20Sopenharmony_ci data->temp_tolerance[nr] = TOL_TEMP_TO_REG(val); 9018c2ecf20Sopenharmony_ci target_mask = w83791d_read(client, 9028c2ecf20Sopenharmony_ci W83791D_REG_TEMP_TOL[reg_idx]) & keep_mask; 9038c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP_TOL[reg_idx], 9048c2ecf20Sopenharmony_ci (data->temp_tolerance[nr] << val_shift) | target_mask); 9058c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 9068c2ecf20Sopenharmony_ci return count; 9078c2ecf20Sopenharmony_ci} 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_temp_tolerance[] = { 9108c2ecf20Sopenharmony_ci SENSOR_ATTR(temp1_tolerance, S_IWUSR | S_IRUGO, 9118c2ecf20Sopenharmony_ci show_temp_tolerance, store_temp_tolerance, 0), 9128c2ecf20Sopenharmony_ci SENSOR_ATTR(temp2_tolerance, S_IWUSR | S_IRUGO, 9138c2ecf20Sopenharmony_ci show_temp_tolerance, store_temp_tolerance, 1), 9148c2ecf20Sopenharmony_ci SENSOR_ATTR(temp3_tolerance, S_IWUSR | S_IRUGO, 9158c2ecf20Sopenharmony_ci show_temp_tolerance, store_temp_tolerance, 2), 9168c2ecf20Sopenharmony_ci}; 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci/* read/write the temperature1, includes measured value and limits */ 9198c2ecf20Sopenharmony_cistatic ssize_t show_temp1(struct device *dev, struct device_attribute *devattr, 9208c2ecf20Sopenharmony_ci char *buf) 9218c2ecf20Sopenharmony_ci{ 9228c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 9238c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 9248c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp1[attr->index])); 9258c2ecf20Sopenharmony_ci} 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_cistatic ssize_t store_temp1(struct device *dev, struct device_attribute *devattr, 9288c2ecf20Sopenharmony_ci const char *buf, size_t count) 9298c2ecf20Sopenharmony_ci{ 9308c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 9318c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 9328c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 9338c2ecf20Sopenharmony_ci int nr = attr->index; 9348c2ecf20Sopenharmony_ci long val; 9358c2ecf20Sopenharmony_ci int err; 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 9388c2ecf20Sopenharmony_ci if (err) 9398c2ecf20Sopenharmony_ci return err; 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 9428c2ecf20Sopenharmony_ci data->temp1[nr] = TEMP1_TO_REG(val); 9438c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP1[nr], data->temp1[nr]); 9448c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 9458c2ecf20Sopenharmony_ci return count; 9468c2ecf20Sopenharmony_ci} 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci/* read/write temperature2-3, includes measured value and limits */ 9498c2ecf20Sopenharmony_cistatic ssize_t show_temp23(struct device *dev, struct device_attribute *devattr, 9508c2ecf20Sopenharmony_ci char *buf) 9518c2ecf20Sopenharmony_ci{ 9528c2ecf20Sopenharmony_ci struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); 9538c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 9548c2ecf20Sopenharmony_ci int nr = attr->nr; 9558c2ecf20Sopenharmony_ci int index = attr->index; 9568c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", TEMP23_FROM_REG(data->temp_add[nr][index])); 9578c2ecf20Sopenharmony_ci} 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_cistatic ssize_t store_temp23(struct device *dev, 9608c2ecf20Sopenharmony_ci struct device_attribute *devattr, 9618c2ecf20Sopenharmony_ci const char *buf, size_t count) 9628c2ecf20Sopenharmony_ci{ 9638c2ecf20Sopenharmony_ci struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); 9648c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 9658c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 9668c2ecf20Sopenharmony_ci long val; 9678c2ecf20Sopenharmony_ci int err; 9688c2ecf20Sopenharmony_ci int nr = attr->nr; 9698c2ecf20Sopenharmony_ci int index = attr->index; 9708c2ecf20Sopenharmony_ci 9718c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 9728c2ecf20Sopenharmony_ci if (err) 9738c2ecf20Sopenharmony_ci return err; 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 9768c2ecf20Sopenharmony_ci data->temp_add[nr][index] = TEMP23_TO_REG(val); 9778c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2], 9788c2ecf20Sopenharmony_ci data->temp_add[nr][index] >> 8); 9798c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2 + 1], 9808c2ecf20Sopenharmony_ci data->temp_add[nr][index] & 0x80); 9818c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_ci return count; 9848c2ecf20Sopenharmony_ci} 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_cistatic struct sensor_device_attribute_2 sda_temp_input[] = { 9878c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp1, NULL, 0, 0), 9888c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp23, NULL, 0, 0), 9898c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp23, NULL, 1, 0), 9908c2ecf20Sopenharmony_ci}; 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_cistatic struct sensor_device_attribute_2 sda_temp_max[] = { 9938c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, 9948c2ecf20Sopenharmony_ci show_temp1, store_temp1, 0, 1), 9958c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, 9968c2ecf20Sopenharmony_ci show_temp23, store_temp23, 0, 1), 9978c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, 9988c2ecf20Sopenharmony_ci show_temp23, store_temp23, 1, 1), 9998c2ecf20Sopenharmony_ci}; 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_cistatic struct sensor_device_attribute_2 sda_temp_max_hyst[] = { 10028c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, 10038c2ecf20Sopenharmony_ci show_temp1, store_temp1, 0, 2), 10048c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, 10058c2ecf20Sopenharmony_ci show_temp23, store_temp23, 0, 2), 10068c2ecf20Sopenharmony_ci SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR, 10078c2ecf20Sopenharmony_ci show_temp23, store_temp23, 1, 2), 10088c2ecf20Sopenharmony_ci}; 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_ci/* 10118c2ecf20Sopenharmony_ci * Note: The bitmask for the beep enable/disable is different than 10128c2ecf20Sopenharmony_ci * the bitmask for the alarm. 10138c2ecf20Sopenharmony_ci */ 10148c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_temp_beep[] = { 10158c2ecf20Sopenharmony_ci SENSOR_ATTR(temp1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 4), 10168c2ecf20Sopenharmony_ci SENSOR_ATTR(temp2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 5), 10178c2ecf20Sopenharmony_ci SENSOR_ATTR(temp3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 1), 10188c2ecf20Sopenharmony_ci}; 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_temp_alarm[] = { 10218c2ecf20Sopenharmony_ci SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4), 10228c2ecf20Sopenharmony_ci SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5), 10238c2ecf20Sopenharmony_ci SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13), 10248c2ecf20Sopenharmony_ci}; 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci/* get realtime status of all sensors items: voltage, temp, fan */ 10278c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *attr, 10288c2ecf20Sopenharmony_ci char *buf) 10298c2ecf20Sopenharmony_ci{ 10308c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 10318c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", data->alarms); 10328c2ecf20Sopenharmony_ci} 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms); 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci/* Beep control */ 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci#define GLOBAL_BEEP_ENABLE_SHIFT 15 10398c2ecf20Sopenharmony_ci#define GLOBAL_BEEP_ENABLE_MASK (1 << GLOBAL_BEEP_ENABLE_SHIFT) 10408c2ecf20Sopenharmony_ci 10418c2ecf20Sopenharmony_cistatic ssize_t show_beep_enable(struct device *dev, 10428c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 10438c2ecf20Sopenharmony_ci{ 10448c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 10458c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->beep_enable); 10468c2ecf20Sopenharmony_ci} 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_cistatic ssize_t show_beep_mask(struct device *dev, 10498c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 10508c2ecf20Sopenharmony_ci{ 10518c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 10528c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", BEEP_MASK_FROM_REG(data->beep_mask)); 10538c2ecf20Sopenharmony_ci} 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_ci 10568c2ecf20Sopenharmony_cistatic ssize_t store_beep_mask(struct device *dev, 10578c2ecf20Sopenharmony_ci struct device_attribute *attr, 10588c2ecf20Sopenharmony_ci const char *buf, size_t count) 10598c2ecf20Sopenharmony_ci{ 10608c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 10618c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 10628c2ecf20Sopenharmony_ci int i; 10638c2ecf20Sopenharmony_ci long val; 10648c2ecf20Sopenharmony_ci int err; 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 10678c2ecf20Sopenharmony_ci if (err) 10688c2ecf20Sopenharmony_ci return err; 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ci /* 10738c2ecf20Sopenharmony_ci * The beep_enable state overrides any enabling request from 10748c2ecf20Sopenharmony_ci * the masks 10758c2ecf20Sopenharmony_ci */ 10768c2ecf20Sopenharmony_ci data->beep_mask = BEEP_MASK_TO_REG(val) & ~GLOBAL_BEEP_ENABLE_MASK; 10778c2ecf20Sopenharmony_ci data->beep_mask |= (data->beep_enable << GLOBAL_BEEP_ENABLE_SHIFT); 10788c2ecf20Sopenharmony_ci 10798c2ecf20Sopenharmony_ci val = data->beep_mask; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) { 10828c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_BEEP_CTRL[i], (val & 0xff)); 10838c2ecf20Sopenharmony_ci val >>= 8; 10848c2ecf20Sopenharmony_ci } 10858c2ecf20Sopenharmony_ci 10868c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 10878c2ecf20Sopenharmony_ci 10888c2ecf20Sopenharmony_ci return count; 10898c2ecf20Sopenharmony_ci} 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_cistatic ssize_t store_beep_enable(struct device *dev, 10928c2ecf20Sopenharmony_ci struct device_attribute *attr, 10938c2ecf20Sopenharmony_ci const char *buf, size_t count) 10948c2ecf20Sopenharmony_ci{ 10958c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 10968c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 10978c2ecf20Sopenharmony_ci long val; 10988c2ecf20Sopenharmony_ci int err; 10998c2ecf20Sopenharmony_ci 11008c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 11018c2ecf20Sopenharmony_ci if (err) 11028c2ecf20Sopenharmony_ci return err; 11038c2ecf20Sopenharmony_ci 11048c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_ci data->beep_enable = val ? 1 : 0; 11078c2ecf20Sopenharmony_ci 11088c2ecf20Sopenharmony_ci /* Keep the full mask value in sync with the current enable */ 11098c2ecf20Sopenharmony_ci data->beep_mask &= ~GLOBAL_BEEP_ENABLE_MASK; 11108c2ecf20Sopenharmony_ci data->beep_mask |= (data->beep_enable << GLOBAL_BEEP_ENABLE_SHIFT); 11118c2ecf20Sopenharmony_ci 11128c2ecf20Sopenharmony_ci /* 11138c2ecf20Sopenharmony_ci * The global control is in the second beep control register 11148c2ecf20Sopenharmony_ci * so only need to update that register 11158c2ecf20Sopenharmony_ci */ 11168c2ecf20Sopenharmony_ci val = (data->beep_mask >> 8) & 0xff; 11178c2ecf20Sopenharmony_ci 11188c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_BEEP_CTRL[1], val); 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 11218c2ecf20Sopenharmony_ci 11228c2ecf20Sopenharmony_ci return count; 11238c2ecf20Sopenharmony_ci} 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_cistatic struct sensor_device_attribute sda_beep_ctrl[] = { 11268c2ecf20Sopenharmony_ci SENSOR_ATTR(beep_enable, S_IRUGO | S_IWUSR, 11278c2ecf20Sopenharmony_ci show_beep_enable, store_beep_enable, 0), 11288c2ecf20Sopenharmony_ci SENSOR_ATTR(beep_mask, S_IRUGO | S_IWUSR, 11298c2ecf20Sopenharmony_ci show_beep_mask, store_beep_mask, 1) 11308c2ecf20Sopenharmony_ci}; 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci/* cpu voltage regulation information */ 11338c2ecf20Sopenharmony_cistatic ssize_t cpu0_vid_show(struct device *dev, 11348c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 11358c2ecf20Sopenharmony_ci{ 11368c2ecf20Sopenharmony_ci struct w83791d_data *data = w83791d_update_device(dev); 11378c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 11388c2ecf20Sopenharmony_ci} 11398c2ecf20Sopenharmony_ci 11408c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid); 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_cistatic ssize_t vrm_show(struct device *dev, struct device_attribute *attr, 11438c2ecf20Sopenharmony_ci char *buf) 11448c2ecf20Sopenharmony_ci{ 11458c2ecf20Sopenharmony_ci struct w83791d_data *data = dev_get_drvdata(dev); 11468c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", data->vrm); 11478c2ecf20Sopenharmony_ci} 11488c2ecf20Sopenharmony_ci 11498c2ecf20Sopenharmony_cistatic ssize_t vrm_store(struct device *dev, struct device_attribute *attr, 11508c2ecf20Sopenharmony_ci const char *buf, size_t count) 11518c2ecf20Sopenharmony_ci{ 11528c2ecf20Sopenharmony_ci struct w83791d_data *data = dev_get_drvdata(dev); 11538c2ecf20Sopenharmony_ci unsigned long val; 11548c2ecf20Sopenharmony_ci int err; 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci /* 11578c2ecf20Sopenharmony_ci * No lock needed as vrm is internal to the driver 11588c2ecf20Sopenharmony_ci * (not read from a chip register) and so is not 11598c2ecf20Sopenharmony_ci * updated in w83791d_update_device() 11608c2ecf20Sopenharmony_ci */ 11618c2ecf20Sopenharmony_ci 11628c2ecf20Sopenharmony_ci err = kstrtoul(buf, 10, &val); 11638c2ecf20Sopenharmony_ci if (err) 11648c2ecf20Sopenharmony_ci return err; 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci if (val > 255) 11678c2ecf20Sopenharmony_ci return -EINVAL; 11688c2ecf20Sopenharmony_ci 11698c2ecf20Sopenharmony_ci data->vrm = val; 11708c2ecf20Sopenharmony_ci return count; 11718c2ecf20Sopenharmony_ci} 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vrm); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci#define IN_UNIT_ATTRS(X) \ 11768c2ecf20Sopenharmony_ci &sda_in_input[X].dev_attr.attr, \ 11778c2ecf20Sopenharmony_ci &sda_in_min[X].dev_attr.attr, \ 11788c2ecf20Sopenharmony_ci &sda_in_max[X].dev_attr.attr, \ 11798c2ecf20Sopenharmony_ci &sda_in_beep[X].dev_attr.attr, \ 11808c2ecf20Sopenharmony_ci &sda_in_alarm[X].dev_attr.attr 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_ci#define FAN_UNIT_ATTRS(X) \ 11838c2ecf20Sopenharmony_ci &sda_fan_input[X].dev_attr.attr, \ 11848c2ecf20Sopenharmony_ci &sda_fan_min[X].dev_attr.attr, \ 11858c2ecf20Sopenharmony_ci &sda_fan_div[X].dev_attr.attr, \ 11868c2ecf20Sopenharmony_ci &sda_fan_beep[X].dev_attr.attr, \ 11878c2ecf20Sopenharmony_ci &sda_fan_alarm[X].dev_attr.attr 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci#define TEMP_UNIT_ATTRS(X) \ 11908c2ecf20Sopenharmony_ci &sda_temp_input[X].dev_attr.attr, \ 11918c2ecf20Sopenharmony_ci &sda_temp_max[X].dev_attr.attr, \ 11928c2ecf20Sopenharmony_ci &sda_temp_max_hyst[X].dev_attr.attr, \ 11938c2ecf20Sopenharmony_ci &sda_temp_beep[X].dev_attr.attr, \ 11948c2ecf20Sopenharmony_ci &sda_temp_alarm[X].dev_attr.attr 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_cistatic struct attribute *w83791d_attributes[] = { 11978c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(0), 11988c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(1), 11998c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(2), 12008c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(3), 12018c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(4), 12028c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(5), 12038c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(6), 12048c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(7), 12058c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(8), 12068c2ecf20Sopenharmony_ci IN_UNIT_ATTRS(9), 12078c2ecf20Sopenharmony_ci FAN_UNIT_ATTRS(0), 12088c2ecf20Sopenharmony_ci FAN_UNIT_ATTRS(1), 12098c2ecf20Sopenharmony_ci FAN_UNIT_ATTRS(2), 12108c2ecf20Sopenharmony_ci TEMP_UNIT_ATTRS(0), 12118c2ecf20Sopenharmony_ci TEMP_UNIT_ATTRS(1), 12128c2ecf20Sopenharmony_ci TEMP_UNIT_ATTRS(2), 12138c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 12148c2ecf20Sopenharmony_ci &sda_beep_ctrl[0].dev_attr.attr, 12158c2ecf20Sopenharmony_ci &sda_beep_ctrl[1].dev_attr.attr, 12168c2ecf20Sopenharmony_ci &dev_attr_cpu0_vid.attr, 12178c2ecf20Sopenharmony_ci &dev_attr_vrm.attr, 12188c2ecf20Sopenharmony_ci &sda_pwm[0].dev_attr.attr, 12198c2ecf20Sopenharmony_ci &sda_pwm[1].dev_attr.attr, 12208c2ecf20Sopenharmony_ci &sda_pwm[2].dev_attr.attr, 12218c2ecf20Sopenharmony_ci &sda_pwmenable[0].dev_attr.attr, 12228c2ecf20Sopenharmony_ci &sda_pwmenable[1].dev_attr.attr, 12238c2ecf20Sopenharmony_ci &sda_pwmenable[2].dev_attr.attr, 12248c2ecf20Sopenharmony_ci &sda_temp_target[0].dev_attr.attr, 12258c2ecf20Sopenharmony_ci &sda_temp_target[1].dev_attr.attr, 12268c2ecf20Sopenharmony_ci &sda_temp_target[2].dev_attr.attr, 12278c2ecf20Sopenharmony_ci &sda_temp_tolerance[0].dev_attr.attr, 12288c2ecf20Sopenharmony_ci &sda_temp_tolerance[1].dev_attr.attr, 12298c2ecf20Sopenharmony_ci &sda_temp_tolerance[2].dev_attr.attr, 12308c2ecf20Sopenharmony_ci NULL 12318c2ecf20Sopenharmony_ci}; 12328c2ecf20Sopenharmony_ci 12338c2ecf20Sopenharmony_cistatic const struct attribute_group w83791d_group = { 12348c2ecf20Sopenharmony_ci .attrs = w83791d_attributes, 12358c2ecf20Sopenharmony_ci}; 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_ci/* 12388c2ecf20Sopenharmony_ci * Separate group of attributes for fan/pwm 4-5. Their pins can also be 12398c2ecf20Sopenharmony_ci * in use for GPIO in which case their sysfs-interface should not be made 12408c2ecf20Sopenharmony_ci * available 12418c2ecf20Sopenharmony_ci */ 12428c2ecf20Sopenharmony_cistatic struct attribute *w83791d_attributes_fanpwm45[] = { 12438c2ecf20Sopenharmony_ci FAN_UNIT_ATTRS(3), 12448c2ecf20Sopenharmony_ci FAN_UNIT_ATTRS(4), 12458c2ecf20Sopenharmony_ci &sda_pwm[3].dev_attr.attr, 12468c2ecf20Sopenharmony_ci &sda_pwm[4].dev_attr.attr, 12478c2ecf20Sopenharmony_ci NULL 12488c2ecf20Sopenharmony_ci}; 12498c2ecf20Sopenharmony_ci 12508c2ecf20Sopenharmony_cistatic const struct attribute_group w83791d_group_fanpwm45 = { 12518c2ecf20Sopenharmony_ci .attrs = w83791d_attributes_fanpwm45, 12528c2ecf20Sopenharmony_ci}; 12538c2ecf20Sopenharmony_ci 12548c2ecf20Sopenharmony_cistatic int w83791d_detect_subclients(struct i2c_client *client) 12558c2ecf20Sopenharmony_ci{ 12568c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 12578c2ecf20Sopenharmony_ci int address = client->addr; 12588c2ecf20Sopenharmony_ci int i, id; 12598c2ecf20Sopenharmony_ci u8 val; 12608c2ecf20Sopenharmony_ci 12618c2ecf20Sopenharmony_ci id = i2c_adapter_id(adapter); 12628c2ecf20Sopenharmony_ci if (force_subclients[0] == id && force_subclients[1] == address) { 12638c2ecf20Sopenharmony_ci for (i = 2; i <= 3; i++) { 12648c2ecf20Sopenharmony_ci if (force_subclients[i] < 0x48 || 12658c2ecf20Sopenharmony_ci force_subclients[i] > 0x4f) { 12668c2ecf20Sopenharmony_ci dev_err(&client->dev, 12678c2ecf20Sopenharmony_ci "invalid subclient " 12688c2ecf20Sopenharmony_ci "address %d; must be 0x48-0x4f\n", 12698c2ecf20Sopenharmony_ci force_subclients[i]); 12708c2ecf20Sopenharmony_ci return -ENODEV; 12718c2ecf20Sopenharmony_ci } 12728c2ecf20Sopenharmony_ci } 12738c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_I2C_SUBADDR, 12748c2ecf20Sopenharmony_ci (force_subclients[2] & 0x07) | 12758c2ecf20Sopenharmony_ci ((force_subclients[3] & 0x07) << 4)); 12768c2ecf20Sopenharmony_ci } 12778c2ecf20Sopenharmony_ci 12788c2ecf20Sopenharmony_ci val = w83791d_read(client, W83791D_REG_I2C_SUBADDR); 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci if (!(val & 0x88) && (val & 0x7) == ((val >> 4) & 0x7)) { 12818c2ecf20Sopenharmony_ci dev_err(&client->dev, 12828c2ecf20Sopenharmony_ci "duplicate addresses 0x%x, use force_subclient\n", 0x48 + (val & 0x7)); 12838c2ecf20Sopenharmony_ci return -ENODEV; 12848c2ecf20Sopenharmony_ci } 12858c2ecf20Sopenharmony_ci 12868c2ecf20Sopenharmony_ci if (!(val & 0x08)) 12878c2ecf20Sopenharmony_ci devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + (val & 0x7)); 12888c2ecf20Sopenharmony_ci 12898c2ecf20Sopenharmony_ci if (!(val & 0x80)) 12908c2ecf20Sopenharmony_ci devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + ((val >> 4) & 0x7)); 12918c2ecf20Sopenharmony_ci 12928c2ecf20Sopenharmony_ci return 0; 12938c2ecf20Sopenharmony_ci} 12948c2ecf20Sopenharmony_ci 12958c2ecf20Sopenharmony_ci 12968c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */ 12978c2ecf20Sopenharmony_cistatic int w83791d_detect(struct i2c_client *client, 12988c2ecf20Sopenharmony_ci struct i2c_board_info *info) 12998c2ecf20Sopenharmony_ci{ 13008c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 13018c2ecf20Sopenharmony_ci int val1, val2; 13028c2ecf20Sopenharmony_ci unsigned short address = client->addr; 13038c2ecf20Sopenharmony_ci 13048c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 13058c2ecf20Sopenharmony_ci return -ENODEV; 13068c2ecf20Sopenharmony_ci 13078c2ecf20Sopenharmony_ci if (w83791d_read(client, W83791D_REG_CONFIG) & 0x80) 13088c2ecf20Sopenharmony_ci return -ENODEV; 13098c2ecf20Sopenharmony_ci 13108c2ecf20Sopenharmony_ci val1 = w83791d_read(client, W83791D_REG_BANK); 13118c2ecf20Sopenharmony_ci val2 = w83791d_read(client, W83791D_REG_CHIPMAN); 13128c2ecf20Sopenharmony_ci /* Check for Winbond ID if in bank 0 */ 13138c2ecf20Sopenharmony_ci if (!(val1 & 0x07)) { 13148c2ecf20Sopenharmony_ci if ((!(val1 & 0x80) && val2 != 0xa3) || 13158c2ecf20Sopenharmony_ci ((val1 & 0x80) && val2 != 0x5c)) { 13168c2ecf20Sopenharmony_ci return -ENODEV; 13178c2ecf20Sopenharmony_ci } 13188c2ecf20Sopenharmony_ci } 13198c2ecf20Sopenharmony_ci /* 13208c2ecf20Sopenharmony_ci * If Winbond chip, address of chip and W83791D_REG_I2C_ADDR 13218c2ecf20Sopenharmony_ci * should match 13228c2ecf20Sopenharmony_ci */ 13238c2ecf20Sopenharmony_ci if (w83791d_read(client, W83791D_REG_I2C_ADDR) != address) 13248c2ecf20Sopenharmony_ci return -ENODEV; 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_ci /* We want bank 0 and Vendor ID high byte */ 13278c2ecf20Sopenharmony_ci val1 = w83791d_read(client, W83791D_REG_BANK) & 0x78; 13288c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_BANK, val1 | 0x80); 13298c2ecf20Sopenharmony_ci 13308c2ecf20Sopenharmony_ci /* Verify it is a Winbond w83791d */ 13318c2ecf20Sopenharmony_ci val1 = w83791d_read(client, W83791D_REG_WCHIPID); 13328c2ecf20Sopenharmony_ci val2 = w83791d_read(client, W83791D_REG_CHIPMAN); 13338c2ecf20Sopenharmony_ci if (val1 != 0x71 || val2 != 0x5c) 13348c2ecf20Sopenharmony_ci return -ENODEV; 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci strlcpy(info->type, "w83791d", I2C_NAME_SIZE); 13378c2ecf20Sopenharmony_ci 13388c2ecf20Sopenharmony_ci return 0; 13398c2ecf20Sopenharmony_ci} 13408c2ecf20Sopenharmony_ci 13418c2ecf20Sopenharmony_cistatic int w83791d_probe(struct i2c_client *client) 13428c2ecf20Sopenharmony_ci{ 13438c2ecf20Sopenharmony_ci struct w83791d_data *data; 13448c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 13458c2ecf20Sopenharmony_ci int i, err; 13468c2ecf20Sopenharmony_ci u8 has_fanpwm45; 13478c2ecf20Sopenharmony_ci 13488c2ecf20Sopenharmony_ci#ifdef DEBUG 13498c2ecf20Sopenharmony_ci int val1; 13508c2ecf20Sopenharmony_ci val1 = w83791d_read(client, W83791D_REG_DID_VID4); 13518c2ecf20Sopenharmony_ci dev_dbg(dev, "Device ID version: %d.%d (0x%02x)\n", 13528c2ecf20Sopenharmony_ci (val1 >> 5) & 0x07, (val1 >> 1) & 0x0f, val1); 13538c2ecf20Sopenharmony_ci#endif 13548c2ecf20Sopenharmony_ci 13558c2ecf20Sopenharmony_ci data = devm_kzalloc(&client->dev, sizeof(struct w83791d_data), 13568c2ecf20Sopenharmony_ci GFP_KERNEL); 13578c2ecf20Sopenharmony_ci if (!data) 13588c2ecf20Sopenharmony_ci return -ENOMEM; 13598c2ecf20Sopenharmony_ci 13608c2ecf20Sopenharmony_ci i2c_set_clientdata(client, data); 13618c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 13628c2ecf20Sopenharmony_ci 13638c2ecf20Sopenharmony_ci err = w83791d_detect_subclients(client); 13648c2ecf20Sopenharmony_ci if (err) 13658c2ecf20Sopenharmony_ci return err; 13668c2ecf20Sopenharmony_ci 13678c2ecf20Sopenharmony_ci /* Initialize the chip */ 13688c2ecf20Sopenharmony_ci w83791d_init_client(client); 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci /* 13718c2ecf20Sopenharmony_ci * If the fan_div is changed, make sure there is a rational 13728c2ecf20Sopenharmony_ci * fan_min in place 13738c2ecf20Sopenharmony_ci */ 13748c2ecf20Sopenharmony_ci for (i = 0; i < NUMBER_OF_FANIN; i++) 13758c2ecf20Sopenharmony_ci data->fan_min[i] = w83791d_read(client, W83791D_REG_FAN_MIN[i]); 13768c2ecf20Sopenharmony_ci 13778c2ecf20Sopenharmony_ci /* Register sysfs hooks */ 13788c2ecf20Sopenharmony_ci err = sysfs_create_group(&client->dev.kobj, &w83791d_group); 13798c2ecf20Sopenharmony_ci if (err) 13808c2ecf20Sopenharmony_ci return err; 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_ci /* Check if pins of fan/pwm 4-5 are in use as GPIO */ 13838c2ecf20Sopenharmony_ci has_fanpwm45 = w83791d_read(client, W83791D_REG_GPIO) & 0x10; 13848c2ecf20Sopenharmony_ci if (has_fanpwm45) { 13858c2ecf20Sopenharmony_ci err = sysfs_create_group(&client->dev.kobj, 13868c2ecf20Sopenharmony_ci &w83791d_group_fanpwm45); 13878c2ecf20Sopenharmony_ci if (err) 13888c2ecf20Sopenharmony_ci goto error4; 13898c2ecf20Sopenharmony_ci } 13908c2ecf20Sopenharmony_ci 13918c2ecf20Sopenharmony_ci /* Everything is ready, now register the working device */ 13928c2ecf20Sopenharmony_ci data->hwmon_dev = hwmon_device_register(dev); 13938c2ecf20Sopenharmony_ci if (IS_ERR(data->hwmon_dev)) { 13948c2ecf20Sopenharmony_ci err = PTR_ERR(data->hwmon_dev); 13958c2ecf20Sopenharmony_ci goto error5; 13968c2ecf20Sopenharmony_ci } 13978c2ecf20Sopenharmony_ci 13988c2ecf20Sopenharmony_ci return 0; 13998c2ecf20Sopenharmony_ci 14008c2ecf20Sopenharmony_cierror5: 14018c2ecf20Sopenharmony_ci if (has_fanpwm45) 14028c2ecf20Sopenharmony_ci sysfs_remove_group(&client->dev.kobj, &w83791d_group_fanpwm45); 14038c2ecf20Sopenharmony_cierror4: 14048c2ecf20Sopenharmony_ci sysfs_remove_group(&client->dev.kobj, &w83791d_group); 14058c2ecf20Sopenharmony_ci return err; 14068c2ecf20Sopenharmony_ci} 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_cistatic int w83791d_remove(struct i2c_client *client) 14098c2ecf20Sopenharmony_ci{ 14108c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 14118c2ecf20Sopenharmony_ci 14128c2ecf20Sopenharmony_ci hwmon_device_unregister(data->hwmon_dev); 14138c2ecf20Sopenharmony_ci sysfs_remove_group(&client->dev.kobj, &w83791d_group); 14148c2ecf20Sopenharmony_ci 14158c2ecf20Sopenharmony_ci return 0; 14168c2ecf20Sopenharmony_ci} 14178c2ecf20Sopenharmony_ci 14188c2ecf20Sopenharmony_cistatic void w83791d_init_client(struct i2c_client *client) 14198c2ecf20Sopenharmony_ci{ 14208c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 14218c2ecf20Sopenharmony_ci u8 tmp; 14228c2ecf20Sopenharmony_ci u8 old_beep; 14238c2ecf20Sopenharmony_ci 14248c2ecf20Sopenharmony_ci /* 14258c2ecf20Sopenharmony_ci * The difference between reset and init is that reset 14268c2ecf20Sopenharmony_ci * does a hard reset of the chip via index 0x40, bit 7, 14278c2ecf20Sopenharmony_ci * but init simply forces certain registers to have "sane" 14288c2ecf20Sopenharmony_ci * values. The hope is that the BIOS has done the right 14298c2ecf20Sopenharmony_ci * thing (which is why the default is reset=0, init=0), 14308c2ecf20Sopenharmony_ci * but if not, reset is the hard hammer and init 14318c2ecf20Sopenharmony_ci * is the soft mallet both of which are trying to whack 14328c2ecf20Sopenharmony_ci * things into place... 14338c2ecf20Sopenharmony_ci * NOTE: The data sheet makes a distinction between 14348c2ecf20Sopenharmony_ci * "power on defaults" and "reset by MR". As far as I can tell, 14358c2ecf20Sopenharmony_ci * the hard reset puts everything into a power-on state so I'm 14368c2ecf20Sopenharmony_ci * not sure what "reset by MR" means or how it can happen. 14378c2ecf20Sopenharmony_ci */ 14388c2ecf20Sopenharmony_ci if (reset || init) { 14398c2ecf20Sopenharmony_ci /* keep some BIOS settings when we... */ 14408c2ecf20Sopenharmony_ci old_beep = w83791d_read(client, W83791D_REG_BEEP_CONFIG); 14418c2ecf20Sopenharmony_ci 14428c2ecf20Sopenharmony_ci if (reset) { 14438c2ecf20Sopenharmony_ci /* ... reset the chip and ... */ 14448c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_CONFIG, 0x80); 14458c2ecf20Sopenharmony_ci } 14468c2ecf20Sopenharmony_ci 14478c2ecf20Sopenharmony_ci /* ... disable power-on abnormal beep */ 14488c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_BEEP_CONFIG, old_beep | 0x80); 14498c2ecf20Sopenharmony_ci 14508c2ecf20Sopenharmony_ci /* disable the global beep (not done by hard reset) */ 14518c2ecf20Sopenharmony_ci tmp = w83791d_read(client, W83791D_REG_BEEP_CTRL[1]); 14528c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_BEEP_CTRL[1], tmp & 0xef); 14538c2ecf20Sopenharmony_ci 14548c2ecf20Sopenharmony_ci if (init) { 14558c2ecf20Sopenharmony_ci /* Make sure monitoring is turned on for add-ons */ 14568c2ecf20Sopenharmony_ci tmp = w83791d_read(client, W83791D_REG_TEMP2_CONFIG); 14578c2ecf20Sopenharmony_ci if (tmp & 1) { 14588c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP2_CONFIG, 14598c2ecf20Sopenharmony_ci tmp & 0xfe); 14608c2ecf20Sopenharmony_ci } 14618c2ecf20Sopenharmony_ci 14628c2ecf20Sopenharmony_ci tmp = w83791d_read(client, W83791D_REG_TEMP3_CONFIG); 14638c2ecf20Sopenharmony_ci if (tmp & 1) { 14648c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_TEMP3_CONFIG, 14658c2ecf20Sopenharmony_ci tmp & 0xfe); 14668c2ecf20Sopenharmony_ci } 14678c2ecf20Sopenharmony_ci 14688c2ecf20Sopenharmony_ci /* Start monitoring */ 14698c2ecf20Sopenharmony_ci tmp = w83791d_read(client, W83791D_REG_CONFIG) & 0xf7; 14708c2ecf20Sopenharmony_ci w83791d_write(client, W83791D_REG_CONFIG, tmp | 0x01); 14718c2ecf20Sopenharmony_ci } 14728c2ecf20Sopenharmony_ci } 14738c2ecf20Sopenharmony_ci 14748c2ecf20Sopenharmony_ci data->vrm = vid_which_vrm(); 14758c2ecf20Sopenharmony_ci} 14768c2ecf20Sopenharmony_ci 14778c2ecf20Sopenharmony_cistatic struct w83791d_data *w83791d_update_device(struct device *dev) 14788c2ecf20Sopenharmony_ci{ 14798c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 14808c2ecf20Sopenharmony_ci struct w83791d_data *data = i2c_get_clientdata(client); 14818c2ecf20Sopenharmony_ci int i, j; 14828c2ecf20Sopenharmony_ci u8 reg_array_tmp[3]; 14838c2ecf20Sopenharmony_ci u8 vbat_reg; 14848c2ecf20Sopenharmony_ci 14858c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 14868c2ecf20Sopenharmony_ci 14878c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated + (HZ * 3)) 14888c2ecf20Sopenharmony_ci || !data->valid) { 14898c2ecf20Sopenharmony_ci dev_dbg(dev, "Starting w83791d device update\n"); 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_ci /* Update the voltages measured value and limits */ 14928c2ecf20Sopenharmony_ci for (i = 0; i < NUMBER_OF_VIN; i++) { 14938c2ecf20Sopenharmony_ci data->in[i] = w83791d_read(client, 14948c2ecf20Sopenharmony_ci W83791D_REG_IN[i]); 14958c2ecf20Sopenharmony_ci data->in_max[i] = w83791d_read(client, 14968c2ecf20Sopenharmony_ci W83791D_REG_IN_MAX[i]); 14978c2ecf20Sopenharmony_ci data->in_min[i] = w83791d_read(client, 14988c2ecf20Sopenharmony_ci W83791D_REG_IN_MIN[i]); 14998c2ecf20Sopenharmony_ci } 15008c2ecf20Sopenharmony_ci 15018c2ecf20Sopenharmony_ci /* Update the fan counts and limits */ 15028c2ecf20Sopenharmony_ci for (i = 0; i < NUMBER_OF_FANIN; i++) { 15038c2ecf20Sopenharmony_ci /* Update the Fan measured value and limits */ 15048c2ecf20Sopenharmony_ci data->fan[i] = w83791d_read(client, 15058c2ecf20Sopenharmony_ci W83791D_REG_FAN[i]); 15068c2ecf20Sopenharmony_ci data->fan_min[i] = w83791d_read(client, 15078c2ecf20Sopenharmony_ci W83791D_REG_FAN_MIN[i]); 15088c2ecf20Sopenharmony_ci } 15098c2ecf20Sopenharmony_ci 15108c2ecf20Sopenharmony_ci /* Update the fan divisor */ 15118c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) { 15128c2ecf20Sopenharmony_ci reg_array_tmp[i] = w83791d_read(client, 15138c2ecf20Sopenharmony_ci W83791D_REG_FAN_DIV[i]); 15148c2ecf20Sopenharmony_ci } 15158c2ecf20Sopenharmony_ci data->fan_div[0] = (reg_array_tmp[0] >> 4) & 0x03; 15168c2ecf20Sopenharmony_ci data->fan_div[1] = (reg_array_tmp[0] >> 6) & 0x03; 15178c2ecf20Sopenharmony_ci data->fan_div[2] = (reg_array_tmp[1] >> 6) & 0x03; 15188c2ecf20Sopenharmony_ci data->fan_div[3] = reg_array_tmp[2] & 0x07; 15198c2ecf20Sopenharmony_ci data->fan_div[4] = (reg_array_tmp[2] >> 4) & 0x07; 15208c2ecf20Sopenharmony_ci 15218c2ecf20Sopenharmony_ci /* 15228c2ecf20Sopenharmony_ci * The fan divisor for fans 0-2 get bit 2 from 15238c2ecf20Sopenharmony_ci * bits 5-7 respectively of vbat register 15248c2ecf20Sopenharmony_ci */ 15258c2ecf20Sopenharmony_ci vbat_reg = w83791d_read(client, W83791D_REG_VBAT); 15268c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) 15278c2ecf20Sopenharmony_ci data->fan_div[i] |= (vbat_reg >> (3 + i)) & 0x04; 15288c2ecf20Sopenharmony_ci 15298c2ecf20Sopenharmony_ci /* Update PWM duty cycle */ 15308c2ecf20Sopenharmony_ci for (i = 0; i < NUMBER_OF_PWM; i++) { 15318c2ecf20Sopenharmony_ci data->pwm[i] = w83791d_read(client, 15328c2ecf20Sopenharmony_ci W83791D_REG_PWM[i]); 15338c2ecf20Sopenharmony_ci } 15348c2ecf20Sopenharmony_ci 15358c2ecf20Sopenharmony_ci /* Update PWM enable status */ 15368c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 15378c2ecf20Sopenharmony_ci reg_array_tmp[i] = w83791d_read(client, 15388c2ecf20Sopenharmony_ci W83791D_REG_FAN_CFG[i]); 15398c2ecf20Sopenharmony_ci } 15408c2ecf20Sopenharmony_ci data->pwm_enable[0] = (reg_array_tmp[0] >> 2) & 0x03; 15418c2ecf20Sopenharmony_ci data->pwm_enable[1] = (reg_array_tmp[0] >> 4) & 0x03; 15428c2ecf20Sopenharmony_ci data->pwm_enable[2] = (reg_array_tmp[1] >> 2) & 0x03; 15438c2ecf20Sopenharmony_ci 15448c2ecf20Sopenharmony_ci /* Update PWM target temperature */ 15458c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) { 15468c2ecf20Sopenharmony_ci data->temp_target[i] = w83791d_read(client, 15478c2ecf20Sopenharmony_ci W83791D_REG_TEMP_TARGET[i]) & 0x7f; 15488c2ecf20Sopenharmony_ci } 15498c2ecf20Sopenharmony_ci 15508c2ecf20Sopenharmony_ci /* Update PWM temperature tolerance */ 15518c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 15528c2ecf20Sopenharmony_ci reg_array_tmp[i] = w83791d_read(client, 15538c2ecf20Sopenharmony_ci W83791D_REG_TEMP_TOL[i]); 15548c2ecf20Sopenharmony_ci } 15558c2ecf20Sopenharmony_ci data->temp_tolerance[0] = reg_array_tmp[0] & 0x0f; 15568c2ecf20Sopenharmony_ci data->temp_tolerance[1] = (reg_array_tmp[0] >> 4) & 0x0f; 15578c2ecf20Sopenharmony_ci data->temp_tolerance[2] = reg_array_tmp[1] & 0x0f; 15588c2ecf20Sopenharmony_ci 15598c2ecf20Sopenharmony_ci /* Update the first temperature sensor */ 15608c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) { 15618c2ecf20Sopenharmony_ci data->temp1[i] = w83791d_read(client, 15628c2ecf20Sopenharmony_ci W83791D_REG_TEMP1[i]); 15638c2ecf20Sopenharmony_ci } 15648c2ecf20Sopenharmony_ci 15658c2ecf20Sopenharmony_ci /* Update the rest of the temperature sensors */ 15668c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 15678c2ecf20Sopenharmony_ci for (j = 0; j < 3; j++) { 15688c2ecf20Sopenharmony_ci data->temp_add[i][j] = 15698c2ecf20Sopenharmony_ci (w83791d_read(client, 15708c2ecf20Sopenharmony_ci W83791D_REG_TEMP_ADD[i][j * 2]) << 8) | 15718c2ecf20Sopenharmony_ci w83791d_read(client, 15728c2ecf20Sopenharmony_ci W83791D_REG_TEMP_ADD[i][j * 2 + 1]); 15738c2ecf20Sopenharmony_ci } 15748c2ecf20Sopenharmony_ci } 15758c2ecf20Sopenharmony_ci 15768c2ecf20Sopenharmony_ci /* Update the realtime status */ 15778c2ecf20Sopenharmony_ci data->alarms = 15788c2ecf20Sopenharmony_ci w83791d_read(client, W83791D_REG_ALARM1) + 15798c2ecf20Sopenharmony_ci (w83791d_read(client, W83791D_REG_ALARM2) << 8) + 15808c2ecf20Sopenharmony_ci (w83791d_read(client, W83791D_REG_ALARM3) << 16); 15818c2ecf20Sopenharmony_ci 15828c2ecf20Sopenharmony_ci /* Update the beep configuration information */ 15838c2ecf20Sopenharmony_ci data->beep_mask = 15848c2ecf20Sopenharmony_ci w83791d_read(client, W83791D_REG_BEEP_CTRL[0]) + 15858c2ecf20Sopenharmony_ci (w83791d_read(client, W83791D_REG_BEEP_CTRL[1]) << 8) + 15868c2ecf20Sopenharmony_ci (w83791d_read(client, W83791D_REG_BEEP_CTRL[2]) << 16); 15878c2ecf20Sopenharmony_ci 15888c2ecf20Sopenharmony_ci /* Extract global beep enable flag */ 15898c2ecf20Sopenharmony_ci data->beep_enable = 15908c2ecf20Sopenharmony_ci (data->beep_mask >> GLOBAL_BEEP_ENABLE_SHIFT) & 0x01; 15918c2ecf20Sopenharmony_ci 15928c2ecf20Sopenharmony_ci /* Update the cpu voltage information */ 15938c2ecf20Sopenharmony_ci i = w83791d_read(client, W83791D_REG_VID_FANDIV); 15948c2ecf20Sopenharmony_ci data->vid = i & 0x0f; 15958c2ecf20Sopenharmony_ci data->vid |= (w83791d_read(client, W83791D_REG_DID_VID4) & 0x01) 15968c2ecf20Sopenharmony_ci << 4; 15978c2ecf20Sopenharmony_ci 15988c2ecf20Sopenharmony_ci data->last_updated = jiffies; 15998c2ecf20Sopenharmony_ci data->valid = 1; 16008c2ecf20Sopenharmony_ci } 16018c2ecf20Sopenharmony_ci 16028c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 16038c2ecf20Sopenharmony_ci 16048c2ecf20Sopenharmony_ci#ifdef DEBUG 16058c2ecf20Sopenharmony_ci w83791d_print_debug(data, dev); 16068c2ecf20Sopenharmony_ci#endif 16078c2ecf20Sopenharmony_ci 16088c2ecf20Sopenharmony_ci return data; 16098c2ecf20Sopenharmony_ci} 16108c2ecf20Sopenharmony_ci 16118c2ecf20Sopenharmony_ci#ifdef DEBUG 16128c2ecf20Sopenharmony_cistatic void w83791d_print_debug(struct w83791d_data *data, struct device *dev) 16138c2ecf20Sopenharmony_ci{ 16148c2ecf20Sopenharmony_ci int i = 0, j = 0; 16158c2ecf20Sopenharmony_ci 16168c2ecf20Sopenharmony_ci dev_dbg(dev, "======Start of w83791d debug values======\n"); 16178c2ecf20Sopenharmony_ci dev_dbg(dev, "%d set of Voltages: ===>\n", NUMBER_OF_VIN); 16188c2ecf20Sopenharmony_ci for (i = 0; i < NUMBER_OF_VIN; i++) { 16198c2ecf20Sopenharmony_ci dev_dbg(dev, "vin[%d] is: 0x%02x\n", i, data->in[i]); 16208c2ecf20Sopenharmony_ci dev_dbg(dev, "vin[%d] min is: 0x%02x\n", i, data->in_min[i]); 16218c2ecf20Sopenharmony_ci dev_dbg(dev, "vin[%d] max is: 0x%02x\n", i, data->in_max[i]); 16228c2ecf20Sopenharmony_ci } 16238c2ecf20Sopenharmony_ci dev_dbg(dev, "%d set of Fan Counts/Divisors: ===>\n", NUMBER_OF_FANIN); 16248c2ecf20Sopenharmony_ci for (i = 0; i < NUMBER_OF_FANIN; i++) { 16258c2ecf20Sopenharmony_ci dev_dbg(dev, "fan[%d] is: 0x%02x\n", i, data->fan[i]); 16268c2ecf20Sopenharmony_ci dev_dbg(dev, "fan[%d] min is: 0x%02x\n", i, data->fan_min[i]); 16278c2ecf20Sopenharmony_ci dev_dbg(dev, "fan_div[%d] is: 0x%02x\n", i, data->fan_div[i]); 16288c2ecf20Sopenharmony_ci } 16298c2ecf20Sopenharmony_ci 16308c2ecf20Sopenharmony_ci /* 16318c2ecf20Sopenharmony_ci * temperature math is signed, but only print out the 16328c2ecf20Sopenharmony_ci * bits that matter 16338c2ecf20Sopenharmony_ci */ 16348c2ecf20Sopenharmony_ci dev_dbg(dev, "%d set of Temperatures: ===>\n", NUMBER_OF_TEMPIN); 16358c2ecf20Sopenharmony_ci for (i = 0; i < 3; i++) 16368c2ecf20Sopenharmony_ci dev_dbg(dev, "temp1[%d] is: 0x%02x\n", i, (u8) data->temp1[i]); 16378c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 16388c2ecf20Sopenharmony_ci for (j = 0; j < 3; j++) { 16398c2ecf20Sopenharmony_ci dev_dbg(dev, "temp_add[%d][%d] is: 0x%04x\n", i, j, 16408c2ecf20Sopenharmony_ci (u16) data->temp_add[i][j]); 16418c2ecf20Sopenharmony_ci } 16428c2ecf20Sopenharmony_ci } 16438c2ecf20Sopenharmony_ci 16448c2ecf20Sopenharmony_ci dev_dbg(dev, "Misc Information: ===>\n"); 16458c2ecf20Sopenharmony_ci dev_dbg(dev, "alarm is: 0x%08x\n", data->alarms); 16468c2ecf20Sopenharmony_ci dev_dbg(dev, "beep_mask is: 0x%08x\n", data->beep_mask); 16478c2ecf20Sopenharmony_ci dev_dbg(dev, "beep_enable is: %d\n", data->beep_enable); 16488c2ecf20Sopenharmony_ci dev_dbg(dev, "vid is: 0x%02x\n", data->vid); 16498c2ecf20Sopenharmony_ci dev_dbg(dev, "vrm is: 0x%02x\n", data->vrm); 16508c2ecf20Sopenharmony_ci dev_dbg(dev, "=======End of w83791d debug values========\n"); 16518c2ecf20Sopenharmony_ci dev_dbg(dev, "\n"); 16528c2ecf20Sopenharmony_ci} 16538c2ecf20Sopenharmony_ci#endif 16548c2ecf20Sopenharmony_ci 16558c2ecf20Sopenharmony_cimodule_i2c_driver(w83791d_driver); 16568c2ecf20Sopenharmony_ci 16578c2ecf20Sopenharmony_ciMODULE_AUTHOR("Charles Spirakis <bezaur@gmail.com>"); 16588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("W83791D driver"); 16598c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1660