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