^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * g760a - Driver for the Global Mixed-mode Technology Inc. G760A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * fan speed PWM controller chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Complete datasheet is available at GMT's website:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * http://www.gmt.com.tw/product/datasheet/EDS-760A.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) enum g760a_regs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) G760A_REG_SET_CNT = 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) G760A_REG_ACT_CNT = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) G760A_REG_FAN_STA = 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define G760A_REG_FAN_STA_RPM_OFF 0x1 /* +/-20% off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define G760A_REG_FAN_STA_RPM_LOW 0x2 /* below 1920rpm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* register data is read (and cached) at most once per second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define G760A_UPDATE_INTERVAL (HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct g760a_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* board specific parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) u32 clk; /* default 32kHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u16 fan_div; /* default P=2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* g760a register cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int valid:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long last_updated; /* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) u8 set_cnt; /* PWM (period) count number; 0xff stops fan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u8 act_cnt; /* formula: cnt = (CLK * 30)/(rpm * P) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u8 fan_sta; /* bit 0: set when actual fan speed more than 20%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * outside requested fan speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * bit 1: set when fan speed below 1920 rpm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define G760A_DEFAULT_CLK 32768
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define G760A_DEFAULT_FAN_DIV 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define PWM_FROM_CNT(cnt) (0xff-(cnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define PWM_TO_CNT(pwm) (0xff-(pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* read/write wrappers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int g760a_write_value(struct i2c_client *client, enum g760a_regs reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return i2c_smbus_write_byte_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * sysfs attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct g760a_data *g760a_update_client(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct g760a_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (time_after(jiffies, data->last_updated + G760A_UPDATE_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) dev_dbg(&client->dev, "Starting g760a update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) data->set_cnt = g760a_read_value(client, G760A_REG_SET_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) data->act_cnt = g760a_read_value(client, G760A_REG_ACT_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) data->fan_sta = g760a_read_value(client, G760A_REG_FAN_STA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static ssize_t fan1_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct g760a_data *data = g760a_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned int rpm = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!(data->fan_sta & G760A_REG_FAN_STA_RPM_LOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) rpm = rpm_from_cnt(data->act_cnt, data->clk, data->fan_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return sprintf(buf, "%d\n", rpm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static ssize_t fan1_alarm_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct g760a_data *data = g760a_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int fan_alarm = (data->fan_sta & G760A_REG_FAN_STA_RPM_OFF) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return sprintf(buf, "%d\n", fan_alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct g760a_data *data = g760a_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return sprintf(buf, "%d\n", PWM_FROM_CNT(data->set_cnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct g760a_data *data = g760a_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (kstrtoul(buf, 10, &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) data->set_cnt = PWM_TO_CNT(clamp_val(val, 0, 255));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static DEVICE_ATTR_RW(pwm1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static DEVICE_ATTR_RO(fan1_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static DEVICE_ATTR_RO(fan1_alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static struct attribute *g760a_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) &dev_attr_pwm1.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) &dev_attr_fan1_input.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) &dev_attr_fan1_alarm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ATTRIBUTE_GROUPS(g760a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * new-style driver model code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int g760a_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct g760a_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) data = devm_kzalloc(dev, sizeof(struct g760a_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* setup default configuration for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) data->fan_div = G760A_DEFAULT_FAN_DIV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) data->clk = G760A_DEFAULT_CLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) g760a_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static const struct i2c_device_id g760a_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) { "g760a", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_DEVICE_TABLE(i2c, g760a_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static struct i2c_driver g760a_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .name = "g760a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .probe_new = g760a_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) .id_table = g760a_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) module_i2c_driver(g760a_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) MODULE_DESCRIPTION("GMT G760A driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) MODULE_LICENSE("GPL");