18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2017 IBM Corp. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Driver for the Nuvoton W83773G SMBus temperature sensor IC. 68c2ecf20Sopenharmony_ci * Supported models: W83773G 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/init.h> 118c2ecf20Sopenharmony_ci#include <linux/i2c.h> 128c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 138c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 148c2ecf20Sopenharmony_ci#include <linux/err.h> 158c2ecf20Sopenharmony_ci#include <linux/of_device.h> 168c2ecf20Sopenharmony_ci#include <linux/regmap.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* W83773 has 3 channels */ 198c2ecf20Sopenharmony_ci#define W83773_CHANNELS 3 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* The W83773 registers */ 228c2ecf20Sopenharmony_ci#define W83773_CONVERSION_RATE_REG_READ 0x04 238c2ecf20Sopenharmony_ci#define W83773_CONVERSION_RATE_REG_WRITE 0x0A 248c2ecf20Sopenharmony_ci#define W83773_MANUFACTURER_ID_REG 0xFE 258c2ecf20Sopenharmony_ci#define W83773_LOCAL_TEMP 0x00 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic const u8 W83773_STATUS[2] = { 0x02, 0x17 }; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic const u8 W83773_TEMP_LSB[2] = { 0x10, 0x25 }; 308c2ecf20Sopenharmony_cistatic const u8 W83773_TEMP_MSB[2] = { 0x01, 0x24 }; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic const u8 W83773_OFFSET_LSB[2] = { 0x12, 0x16 }; 338c2ecf20Sopenharmony_cistatic const u8 W83773_OFFSET_MSB[2] = { 0x11, 0x15 }; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* this is the number of sensors in the device */ 368c2ecf20Sopenharmony_cistatic const struct i2c_device_id w83773_id[] = { 378c2ecf20Sopenharmony_ci { "w83773g" }, 388c2ecf20Sopenharmony_ci { } 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, w83773_id); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused w83773_of_match[] = { 448c2ecf20Sopenharmony_ci { 458c2ecf20Sopenharmony_ci .compatible = "nuvoton,w83773g" 468c2ecf20Sopenharmony_ci }, 478c2ecf20Sopenharmony_ci { }, 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, w83773_of_match); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic inline long temp_of_local(s8 reg) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci return reg * 1000; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic inline long temp_of_remote(s8 hb, u8 lb) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci return (hb << 3 | lb >> 5) * 125; 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic int get_local_temp(struct regmap *regmap, long *val) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci unsigned int regval; 648c2ecf20Sopenharmony_ci int ret; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_LOCAL_TEMP, ®val); 678c2ecf20Sopenharmony_ci if (ret < 0) 688c2ecf20Sopenharmony_ci return ret; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci *val = temp_of_local(regval); 718c2ecf20Sopenharmony_ci return 0; 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic int get_remote_temp(struct regmap *regmap, int index, long *val) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci unsigned int regval_high; 778c2ecf20Sopenharmony_ci unsigned int regval_low; 788c2ecf20Sopenharmony_ci int ret; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_TEMP_MSB[index], ®val_high); 818c2ecf20Sopenharmony_ci if (ret < 0) 828c2ecf20Sopenharmony_ci return ret; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_TEMP_LSB[index], ®val_low); 858c2ecf20Sopenharmony_ci if (ret < 0) 868c2ecf20Sopenharmony_ci return ret; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci *val = temp_of_remote(regval_high, regval_low); 898c2ecf20Sopenharmony_ci return 0; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic int get_fault(struct regmap *regmap, int index, long *val) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci unsigned int regval; 958c2ecf20Sopenharmony_ci int ret; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_STATUS[index], ®val); 988c2ecf20Sopenharmony_ci if (ret < 0) 998c2ecf20Sopenharmony_ci return ret; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci *val = (regval & 0x04) >> 2; 1028c2ecf20Sopenharmony_ci return 0; 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic int get_offset(struct regmap *regmap, int index, long *val) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci unsigned int regval_high; 1088c2ecf20Sopenharmony_ci unsigned int regval_low; 1098c2ecf20Sopenharmony_ci int ret; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_OFFSET_MSB[index], ®val_high); 1128c2ecf20Sopenharmony_ci if (ret < 0) 1138c2ecf20Sopenharmony_ci return ret; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_OFFSET_LSB[index], ®val_low); 1168c2ecf20Sopenharmony_ci if (ret < 0) 1178c2ecf20Sopenharmony_ci return ret; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci *val = temp_of_remote(regval_high, regval_low); 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic int set_offset(struct regmap *regmap, int index, long val) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci int ret; 1268c2ecf20Sopenharmony_ci u8 high_byte; 1278c2ecf20Sopenharmony_ci u8 low_byte; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci val = clamp_val(val, -127825, 127825); 1308c2ecf20Sopenharmony_ci /* offset value equals to (high_byte << 3 | low_byte >> 5) * 125 */ 1318c2ecf20Sopenharmony_ci val /= 125; 1328c2ecf20Sopenharmony_ci high_byte = val >> 3; 1338c2ecf20Sopenharmony_ci low_byte = (val & 0x07) << 5; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci ret = regmap_write(regmap, W83773_OFFSET_MSB[index], high_byte); 1368c2ecf20Sopenharmony_ci if (ret < 0) 1378c2ecf20Sopenharmony_ci return ret; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci return regmap_write(regmap, W83773_OFFSET_LSB[index], low_byte); 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int get_update_interval(struct regmap *regmap, long *val) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci unsigned int regval; 1458c2ecf20Sopenharmony_ci int ret; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci ret = regmap_read(regmap, W83773_CONVERSION_RATE_REG_READ, ®val); 1488c2ecf20Sopenharmony_ci if (ret < 0) 1498c2ecf20Sopenharmony_ci return ret; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci *val = 16000 >> regval; 1528c2ecf20Sopenharmony_ci return 0; 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic int set_update_interval(struct regmap *regmap, long val) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci int rate; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* 1608c2ecf20Sopenharmony_ci * For valid rates, interval can be calculated as 1618c2ecf20Sopenharmony_ci * interval = (1 << (8 - rate)) * 62.5; 1628c2ecf20Sopenharmony_ci * Rounded rate is therefore 1638c2ecf20Sopenharmony_ci * rate = 8 - __fls(interval * 8 / (62.5 * 7)); 1648c2ecf20Sopenharmony_ci * Use clamp_val() to avoid overflows, and to ensure valid input 1658c2ecf20Sopenharmony_ci * for __fls. 1668c2ecf20Sopenharmony_ci */ 1678c2ecf20Sopenharmony_ci val = clamp_val(val, 62, 16000) * 10; 1688c2ecf20Sopenharmony_ci rate = 8 - __fls((val * 8 / (625 * 7))); 1698c2ecf20Sopenharmony_ci return regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, rate); 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic int w83773_read(struct device *dev, enum hwmon_sensor_types type, 1738c2ecf20Sopenharmony_ci u32 attr, int channel, long *val) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci struct regmap *regmap = dev_get_drvdata(dev); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci if (type == hwmon_chip) { 1788c2ecf20Sopenharmony_ci if (attr == hwmon_chip_update_interval) 1798c2ecf20Sopenharmony_ci return get_update_interval(regmap, val); 1808c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci switch (attr) { 1848c2ecf20Sopenharmony_ci case hwmon_temp_input: 1858c2ecf20Sopenharmony_ci if (channel == 0) 1868c2ecf20Sopenharmony_ci return get_local_temp(regmap, val); 1878c2ecf20Sopenharmony_ci return get_remote_temp(regmap, channel - 1, val); 1888c2ecf20Sopenharmony_ci case hwmon_temp_fault: 1898c2ecf20Sopenharmony_ci return get_fault(regmap, channel - 1, val); 1908c2ecf20Sopenharmony_ci case hwmon_temp_offset: 1918c2ecf20Sopenharmony_ci return get_offset(regmap, channel - 1, val); 1928c2ecf20Sopenharmony_ci default: 1938c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 1948c2ecf20Sopenharmony_ci } 1958c2ecf20Sopenharmony_ci} 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_cistatic int w83773_write(struct device *dev, enum hwmon_sensor_types type, 1988c2ecf20Sopenharmony_ci u32 attr, int channel, long val) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci struct regmap *regmap = dev_get_drvdata(dev); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci if (type == hwmon_chip && attr == hwmon_chip_update_interval) 2038c2ecf20Sopenharmony_ci return set_update_interval(regmap, val); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci if (type == hwmon_temp && attr == hwmon_temp_offset) 2068c2ecf20Sopenharmony_ci return set_offset(regmap, channel - 1, val); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cistatic umode_t w83773_is_visible(const void *data, enum hwmon_sensor_types type, 2128c2ecf20Sopenharmony_ci u32 attr, int channel) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci switch (type) { 2158c2ecf20Sopenharmony_ci case hwmon_chip: 2168c2ecf20Sopenharmony_ci switch (attr) { 2178c2ecf20Sopenharmony_ci case hwmon_chip_update_interval: 2188c2ecf20Sopenharmony_ci return 0644; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci break; 2218c2ecf20Sopenharmony_ci case hwmon_temp: 2228c2ecf20Sopenharmony_ci switch (attr) { 2238c2ecf20Sopenharmony_ci case hwmon_temp_input: 2248c2ecf20Sopenharmony_ci case hwmon_temp_fault: 2258c2ecf20Sopenharmony_ci return 0444; 2268c2ecf20Sopenharmony_ci case hwmon_temp_offset: 2278c2ecf20Sopenharmony_ci return 0644; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci break; 2308c2ecf20Sopenharmony_ci default: 2318c2ecf20Sopenharmony_ci break; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci return 0; 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic const struct hwmon_channel_info *w83773_info[] = { 2378c2ecf20Sopenharmony_ci HWMON_CHANNEL_INFO(chip, 2388c2ecf20Sopenharmony_ci HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL), 2398c2ecf20Sopenharmony_ci HWMON_CHANNEL_INFO(temp, 2408c2ecf20Sopenharmony_ci HWMON_T_INPUT, 2418c2ecf20Sopenharmony_ci HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET, 2428c2ecf20Sopenharmony_ci HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET), 2438c2ecf20Sopenharmony_ci NULL 2448c2ecf20Sopenharmony_ci}; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic const struct hwmon_ops w83773_ops = { 2478c2ecf20Sopenharmony_ci .is_visible = w83773_is_visible, 2488c2ecf20Sopenharmony_ci .read = w83773_read, 2498c2ecf20Sopenharmony_ci .write = w83773_write, 2508c2ecf20Sopenharmony_ci}; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic const struct hwmon_chip_info w83773_chip_info = { 2538c2ecf20Sopenharmony_ci .ops = &w83773_ops, 2548c2ecf20Sopenharmony_ci .info = w83773_info, 2558c2ecf20Sopenharmony_ci}; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_cistatic const struct regmap_config w83773_regmap_config = { 2588c2ecf20Sopenharmony_ci .reg_bits = 8, 2598c2ecf20Sopenharmony_ci .val_bits = 8, 2608c2ecf20Sopenharmony_ci}; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_cistatic int w83773_probe(struct i2c_client *client) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 2658c2ecf20Sopenharmony_ci struct device *hwmon_dev; 2668c2ecf20Sopenharmony_ci struct regmap *regmap; 2678c2ecf20Sopenharmony_ci int ret; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci regmap = devm_regmap_init_i2c(client, &w83773_regmap_config); 2708c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) { 2718c2ecf20Sopenharmony_ci dev_err(dev, "failed to allocate register map\n"); 2728c2ecf20Sopenharmony_ci return PTR_ERR(regmap); 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci /* Set the conversion rate to 2 Hz */ 2768c2ecf20Sopenharmony_ci ret = regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, 0x05); 2778c2ecf20Sopenharmony_ci if (ret < 0) { 2788c2ecf20Sopenharmony_ci dev_err(&client->dev, "error writing config rate register\n"); 2798c2ecf20Sopenharmony_ci return ret; 2808c2ecf20Sopenharmony_ci } 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci i2c_set_clientdata(client, regmap); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_info(dev, 2858c2ecf20Sopenharmony_ci client->name, 2868c2ecf20Sopenharmony_ci regmap, 2878c2ecf20Sopenharmony_ci &w83773_chip_info, 2888c2ecf20Sopenharmony_ci NULL); 2898c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic struct i2c_driver w83773_driver = { 2938c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 2948c2ecf20Sopenharmony_ci .driver = { 2958c2ecf20Sopenharmony_ci .name = "w83773g", 2968c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(w83773_of_match), 2978c2ecf20Sopenharmony_ci }, 2988c2ecf20Sopenharmony_ci .probe_new = w83773_probe, 2998c2ecf20Sopenharmony_ci .id_table = w83773_id, 3008c2ecf20Sopenharmony_ci}; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_cimodule_i2c_driver(w83773_driver); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ciMODULE_AUTHOR("Lei YU <mine260309@gmail.com>"); 3058c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("W83773G temperature sensor driver"); 3068c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 307