blob: ea6480b80e7fcd3df07eb80c91a52e9620b9019f [file] [log] [blame]
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -07001/*
Guenter Roeck1b9c4912012-01-19 11:02:18 -08002 * 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 */
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070015
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
Guenter Roeck1b9c4912012-01-19 11:02:18 -080062 * bit 1: set when fan speed below 1920 rpm
63 */
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070064};
65
66#define G760A_DEFAULT_CLK 32768
67#define G760A_DEFAULT_FAN_DIV 2
68
69#define PWM_FROM_CNT(cnt) (0xff-(cnt))
70#define PWM_TO_CNT(pwm) (0xff-(pwm))
71
Jean Delvareebec05b2010-03-05 22:17:22 +010072static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070073{
74 return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
75}
76
77/* new-style driver model */
78static int g760a_probe(struct i2c_client *client,
79 const struct i2c_device_id *id);
80static int g760a_remove(struct i2c_client *client);
81
82static struct i2c_driver g760a_driver = {
83 .driver = {
84 .name = "g760a",
85 },
86 .probe = g760a_probe,
87 .remove = g760a_remove,
88 .id_table = g760a_id,
89};
90
91/* read/write wrappers */
92static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
93{
94 return i2c_smbus_read_byte_data(client, reg);
95}
96
97static int g760a_write_value(struct i2c_client *client, enum g760a_regs reg,
98 u16 value)
99{
100 return i2c_smbus_write_byte_data(client, reg, value);
101}
102
Guenter Roeck1b9c4912012-01-19 11:02:18 -0800103/*
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700104 * sysfs attributes
105 */
106
107static struct g760a_data *g760a_update_client(struct device *dev)
108{
109 struct i2c_client *client = to_i2c_client(dev);
110 struct g760a_data *data = i2c_get_clientdata(client);
111
112 mutex_lock(&data->update_lock);
113
114 if (time_after(jiffies, data->last_updated + G760A_UPDATE_INTERVAL)
115 || !data->valid) {
116 dev_dbg(&client->dev, "Starting g760a update\n");
117
118 data->set_cnt = g760a_read_value(client, G760A_REG_SET_CNT);
119 data->act_cnt = g760a_read_value(client, G760A_REG_ACT_CNT);
120 data->fan_sta = g760a_read_value(client, G760A_REG_FAN_STA);
121
122 data->last_updated = jiffies;
123 data->valid = 1;
124 }
125
126 mutex_unlock(&data->update_lock);
127
128 return data;
129}
130
131static ssize_t show_fan(struct device *dev, struct device_attribute *da,
132 char *buf)
133{
134 struct g760a_data *data = g760a_update_client(dev);
135 unsigned int rpm = 0;
136
137 mutex_lock(&data->update_lock);
138 if (!(data->fan_sta & G760A_REG_FAN_STA_RPM_LOW))
139 rpm = rpm_from_cnt(data->act_cnt, data->clk, data->fan_div);
140 mutex_unlock(&data->update_lock);
141
142 return sprintf(buf, "%d\n", rpm);
143}
144
145static ssize_t show_fan_alarm(struct device *dev, struct device_attribute *da,
146 char *buf)
147{
148 struct g760a_data *data = g760a_update_client(dev);
149
150 int fan_alarm = (data->fan_sta & G760A_REG_FAN_STA_RPM_OFF) ? 1 : 0;
151
152 return sprintf(buf, "%d\n", fan_alarm);
153}
154
155static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
156 char *buf)
157{
158 struct g760a_data *data = g760a_update_client(dev);
159
160 return sprintf(buf, "%d\n", PWM_FROM_CNT(data->set_cnt));
161}
162
163static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
164 const char *buf, size_t count)
165{
166 struct i2c_client *client = to_i2c_client(dev);
167 struct g760a_data *data = g760a_update_client(dev);
168 unsigned long val;
169
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100170 if (kstrtoul(buf, 10, &val))
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700171 return -EINVAL;
172
173 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800174 data->set_cnt = PWM_TO_CNT(clamp_val(val, 0, 255));
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700175 g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
176 mutex_unlock(&data->update_lock);
177
178 return count;
179}
180
181static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
182static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
183static DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL);
184
185static struct attribute *g760a_attributes[] = {
186 &dev_attr_pwm1.attr,
187 &dev_attr_fan1_input.attr,
188 &dev_attr_fan1_alarm.attr,
189 NULL
190};
191
192static const struct attribute_group g760a_group = {
193 .attrs = g760a_attributes,
194};
195
Guenter Roeck1b9c4912012-01-19 11:02:18 -0800196/*
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700197 * new-style driver model code
198 */
199
200static int g760a_probe(struct i2c_client *client,
201 const struct i2c_device_id *id)
202{
203 struct g760a_data *data;
204 int err;
205
206 if (!i2c_check_functionality(client->adapter,
207 I2C_FUNC_SMBUS_BYTE_DATA))
208 return -EIO;
209
Guenter Roecka81b0f732012-06-02 09:58:06 -0700210 data = devm_kzalloc(&client->dev, sizeof(struct g760a_data),
211 GFP_KERNEL);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700212 if (!data)
213 return -ENOMEM;
214
215 i2c_set_clientdata(client, data);
216
217 data->client = client;
218 mutex_init(&data->update_lock);
219
220 /* setup default configuration for now */
221 data->fan_div = G760A_DEFAULT_FAN_DIV;
222 data->clk = G760A_DEFAULT_CLK;
223
224 /* Register sysfs hooks */
225 err = sysfs_create_group(&client->dev.kobj, &g760a_group);
226 if (err)
Guenter Roecka81b0f732012-06-02 09:58:06 -0700227 return err;
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700228
229 data->hwmon_dev = hwmon_device_register(&client->dev);
230 if (IS_ERR(data->hwmon_dev)) {
231 err = PTR_ERR(data->hwmon_dev);
232 goto error_hwmon_device_register;
233 }
234
235 return 0;
236
237error_hwmon_device_register:
238 sysfs_remove_group(&client->dev.kobj, &g760a_group);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700239 return err;
240}
241
242static int g760a_remove(struct i2c_client *client)
243{
244 struct g760a_data *data = i2c_get_clientdata(client);
245 hwmon_device_unregister(data->hwmon_dev);
246 sysfs_remove_group(&client->dev.kobj, &g760a_group);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700247 return 0;
248}
249
Axel Linf0967ee2012-01-20 15:38:18 +0800250module_i2c_driver(g760a_driver);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700251
252MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
253MODULE_DESCRIPTION("GMT G760A driver");
254MODULE_LICENSE("GPL");