Loading...
1/*
2 g760a - Driver for the Global Mixed-mode Technology Inc. G760A
3 fan speed PWM controller chip
4
5 Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
6
7 Complete datasheet is available at GMT's website:
8 http://www.gmt.com.tw/product/datasheet/EDS-760A.pdf
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14*/
15
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/jiffies.h>
20#include <linux/i2c.h>
21#include <linux/hwmon.h>
22#include <linux/hwmon-sysfs.h>
23#include <linux/err.h>
24#include <linux/mutex.h>
25#include <linux/sysfs.h>
26
27static const struct i2c_device_id g760a_id[] = {
28 { "g760a", 0 },
29 { }
30};
31MODULE_DEVICE_TABLE(i2c, g760a_id);
32
33enum g760a_regs {
34 G760A_REG_SET_CNT = 0x00,
35 G760A_REG_ACT_CNT = 0x01,
36 G760A_REG_FAN_STA = 0x02
37};
38
39#define G760A_REG_FAN_STA_RPM_OFF 0x1 /* +/-20% off */
40#define G760A_REG_FAN_STA_RPM_LOW 0x2 /* below 1920rpm */
41
42/* register data is read (and cached) at most once per second */
43#define G760A_UPDATE_INTERVAL (HZ)
44
45struct g760a_data {
46 struct i2c_client *client;
47 struct device *hwmon_dev;
48 struct mutex update_lock;
49
50 /* board specific parameters */
51 u32 clk; /* default 32kHz */
52 u16 fan_div; /* default P=2 */
53
54 /* g760a register cache */
55 unsigned int valid:1;
56 unsigned long last_updated; /* In jiffies */
57
58 u8 set_cnt; /* PWM (period) count number; 0xff stops fan */
59 u8 act_cnt; /* formula: cnt = (CLK * 30)/(rpm * P) */
60 u8 fan_sta; /* bit 0: set when actual fan speed more than 20%
61 * outside requested fan speed
62 * bit 1: set when fan speed below 1920 rpm */
63};
64
65#define G760A_DEFAULT_CLK 32768
66#define G760A_DEFAULT_FAN_DIV 2
67
68#define PWM_FROM_CNT(cnt) (0xff-(cnt))
69#define PWM_TO_CNT(pwm) (0xff-(pwm))
70
71static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
72{
73 return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
74}
75
76/* new-style driver model */
77static int g760a_probe(struct i2c_client *client,
78 const struct i2c_device_id *id);
79static int g760a_remove(struct i2c_client *client);
80
81static struct i2c_driver g760a_driver = {
82 .driver = {
83 .name = "g760a",
84 },
85 .probe = g760a_probe,
86 .remove = g760a_remove,
87 .id_table = g760a_id,
88};
89
90/* read/write wrappers */
91static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
92{
93 return i2c_smbus_read_byte_data(client, reg);
94}
95
96static int g760a_write_value(struct i2c_client *client, enum g760a_regs reg,
97 u16 value)
98{
99 return i2c_smbus_write_byte_data(client, reg, value);
100}
101
102/****************************************************************************
103 * sysfs attributes
104 */
105
106static struct g760a_data *g760a_update_client(struct device *dev)
107{
108 struct i2c_client *client = to_i2c_client(dev);
109 struct g760a_data *data = i2c_get_clientdata(client);
110
111 mutex_lock(&data->update_lock);
112
113 if (time_after(jiffies, data->last_updated + G760A_UPDATE_INTERVAL)
114 || !data->valid) {
115 dev_dbg(&client->dev, "Starting g760a update\n");
116
117 data->set_cnt = g760a_read_value(client, G760A_REG_SET_CNT);
118 data->act_cnt = g760a_read_value(client, G760A_REG_ACT_CNT);
119 data->fan_sta = g760a_read_value(client, G760A_REG_FAN_STA);
120
121 data->last_updated = jiffies;
122 data->valid = 1;
123 }
124
125 mutex_unlock(&data->update_lock);
126
127 return data;
128}
129
130static ssize_t show_fan(struct device *dev, struct device_attribute *da,
131 char *buf)
132{
133 struct g760a_data *data = g760a_update_client(dev);
134 unsigned int rpm = 0;
135
136 mutex_lock(&data->update_lock);
137 if (!(data->fan_sta & G760A_REG_FAN_STA_RPM_LOW))
138 rpm = rpm_from_cnt(data->act_cnt, data->clk, data->fan_div);
139 mutex_unlock(&data->update_lock);
140
141 return sprintf(buf, "%d\n", rpm);
142}
143
144static ssize_t show_fan_alarm(struct device *dev, struct device_attribute *da,
145 char *buf)
146{
147 struct g760a_data *data = g760a_update_client(dev);
148
149 int fan_alarm = (data->fan_sta & G760A_REG_FAN_STA_RPM_OFF) ? 1 : 0;
150
151 return sprintf(buf, "%d\n", fan_alarm);
152}
153
154static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
155 char *buf)
156{
157 struct g760a_data *data = g760a_update_client(dev);
158
159 return sprintf(buf, "%d\n", PWM_FROM_CNT(data->set_cnt));
160}
161
162static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
163 const char *buf, size_t count)
164{
165 struct i2c_client *client = to_i2c_client(dev);
166 struct g760a_data *data = g760a_update_client(dev);
167 unsigned long val;
168
169 if (strict_strtoul(buf, 10, &val))
170 return -EINVAL;
171
172 mutex_lock(&data->update_lock);
173 data->set_cnt = PWM_TO_CNT(SENSORS_LIMIT(val, 0, 255));
174 g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
175 mutex_unlock(&data->update_lock);
176
177 return count;
178}
179
180static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
181static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
182static DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL);
183
184static struct attribute *g760a_attributes[] = {
185 &dev_attr_pwm1.attr,
186 &dev_attr_fan1_input.attr,
187 &dev_attr_fan1_alarm.attr,
188 NULL
189};
190
191static const struct attribute_group g760a_group = {
192 .attrs = g760a_attributes,
193};
194
195/****************************************************************************
196 * new-style driver model code
197 */
198
199static int g760a_probe(struct i2c_client *client,
200 const struct i2c_device_id *id)
201{
202 struct g760a_data *data;
203 int err;
204
205 if (!i2c_check_functionality(client->adapter,
206 I2C_FUNC_SMBUS_BYTE_DATA))
207 return -EIO;
208
209 data = kzalloc(sizeof(struct g760a_data), GFP_KERNEL);
210 if (!data)
211 return -ENOMEM;
212
213 i2c_set_clientdata(client, data);
214
215 data->client = client;
216 mutex_init(&data->update_lock);
217
218 /* setup default configuration for now */
219 data->fan_div = G760A_DEFAULT_FAN_DIV;
220 data->clk = G760A_DEFAULT_CLK;
221
222 /* Register sysfs hooks */
223 err = sysfs_create_group(&client->dev.kobj, &g760a_group);
224 if (err)
225 goto error_sysfs_create_group;
226
227 data->hwmon_dev = hwmon_device_register(&client->dev);
228 if (IS_ERR(data->hwmon_dev)) {
229 err = PTR_ERR(data->hwmon_dev);
230 goto error_hwmon_device_register;
231 }
232
233 return 0;
234
235error_hwmon_device_register:
236 sysfs_remove_group(&client->dev.kobj, &g760a_group);
237error_sysfs_create_group:
238 kfree(data);
239
240 return err;
241}
242
243static int g760a_remove(struct i2c_client *client)
244{
245 struct g760a_data *data = i2c_get_clientdata(client);
246 hwmon_device_unregister(data->hwmon_dev);
247 sysfs_remove_group(&client->dev.kobj, &g760a_group);
248 kfree(data);
249
250 return 0;
251}
252
253/* module management */
254
255static int __init g760a_init(void)
256{
257 return i2c_add_driver(&g760a_driver);
258}
259
260static void __exit g760a_exit(void)
261{
262 i2c_del_driver(&g760a_driver);
263}
264
265MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
266MODULE_DESCRIPTION("GMT G760A driver");
267MODULE_LICENSE("GPL");
268
269module_init(g760a_init);
270module_exit(g760a_exit);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
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
10 */
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
23enum 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
35struct g760a_data {
36 struct i2c_client *client;
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
51 * bit 1: set when fan speed below 1920 rpm
52 */
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
61static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
62{
63 return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
64}
65
66/* read/write wrappers */
67static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
68{
69 return i2c_smbus_read_byte_data(client, reg);
70}
71
72static 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
78/*
79 * sysfs attributes
80 */
81
82static struct g760a_data *g760a_update_client(struct device *dev)
83{
84 struct g760a_data *data = dev_get_drvdata(dev);
85 struct i2c_client *client = data->client;
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 = true;
99 }
100
101 mutex_unlock(&data->update_lock);
102
103 return data;
104}
105
106static ssize_t fan1_input_show(struct device *dev,
107 struct device_attribute *da, char *buf)
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
120static ssize_t fan1_alarm_show(struct device *dev,
121 struct device_attribute *da, char *buf)
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
130static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
131 char *buf)
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
138static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
139 const char *buf, size_t count)
140{
141 struct g760a_data *data = g760a_update_client(dev);
142 struct i2c_client *client = data->client;
143 unsigned long val;
144
145 if (kstrtoul(buf, 10, &val))
146 return -EINVAL;
147
148 mutex_lock(&data->update_lock);
149 data->set_cnt = PWM_TO_CNT(clamp_val(val, 0, 255));
150 g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
151 mutex_unlock(&data->update_lock);
152
153 return count;
154}
155
156static DEVICE_ATTR_RW(pwm1);
157static DEVICE_ATTR_RO(fan1_input);
158static DEVICE_ATTR_RO(fan1_alarm);
159
160static struct attribute *g760a_attrs[] = {
161 &dev_attr_pwm1.attr,
162 &dev_attr_fan1_input.attr,
163 &dev_attr_fan1_alarm.attr,
164 NULL
165};
166
167ATTRIBUTE_GROUPS(g760a);
168
169/*
170 * new-style driver model code
171 */
172
173static int g760a_probe(struct i2c_client *client)
174{
175 struct device *dev = &client->dev;
176 struct g760a_data *data;
177 struct device *hwmon_dev;
178
179 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
180 return -EIO;
181
182 data = devm_kzalloc(dev, sizeof(struct g760a_data), GFP_KERNEL);
183 if (!data)
184 return -ENOMEM;
185
186 data->client = client;
187 mutex_init(&data->update_lock);
188
189 /* setup default configuration for now */
190 data->fan_div = G760A_DEFAULT_FAN_DIV;
191 data->clk = G760A_DEFAULT_CLK;
192
193 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
194 data,
195 g760a_groups);
196 return PTR_ERR_OR_ZERO(hwmon_dev);
197}
198
199static const struct i2c_device_id g760a_id[] = {
200 { "g760a" },
201 { }
202};
203MODULE_DEVICE_TABLE(i2c, g760a_id);
204
205static struct i2c_driver g760a_driver = {
206 .driver = {
207 .name = "g760a",
208 },
209 .probe = g760a_probe,
210 .id_table = g760a_id,
211};
212
213module_i2c_driver(g760a_driver);
214
215MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
216MODULE_DESCRIPTION("GMT G760A driver");
217MODULE_LICENSE("GPL");