blob: ec6a77da411a19fac3a762522670b0b808b30e21 [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
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070027enum g760a_regs {
28 G760A_REG_SET_CNT = 0x00,
29 G760A_REG_ACT_CNT = 0x01,
30 G760A_REG_FAN_STA = 0x02
31};
32
33#define G760A_REG_FAN_STA_RPM_OFF 0x1 /* +/-20% off */
34#define G760A_REG_FAN_STA_RPM_LOW 0x2 /* below 1920rpm */
35
36/* register data is read (and cached) at most once per second */
37#define G760A_UPDATE_INTERVAL (HZ)
38
39struct g760a_data {
40 struct i2c_client *client;
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070041 struct mutex update_lock;
42
43 /* board specific parameters */
44 u32 clk; /* default 32kHz */
45 u16 fan_div; /* default P=2 */
46
47 /* g760a register cache */
48 unsigned int valid:1;
49 unsigned long last_updated; /* In jiffies */
50
51 u8 set_cnt; /* PWM (period) count number; 0xff stops fan */
52 u8 act_cnt; /* formula: cnt = (CLK * 30)/(rpm * P) */
53 u8 fan_sta; /* bit 0: set when actual fan speed more than 20%
54 * outside requested fan speed
Guenter Roeck1b9c4912012-01-19 11:02:18 -080055 * bit 1: set when fan speed below 1920 rpm
56 */
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070057};
58
59#define G760A_DEFAULT_CLK 32768
60#define G760A_DEFAULT_FAN_DIV 2
61
62#define PWM_FROM_CNT(cnt) (0xff-(cnt))
63#define PWM_TO_CNT(pwm) (0xff-(pwm))
64
Jean Delvareebec05b2010-03-05 22:17:22 +010065static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070066{
67 return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
68}
69
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070070/* read/write wrappers */
71static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
72{
73 return i2c_smbus_read_byte_data(client, reg);
74}
75
76static int g760a_write_value(struct i2c_client *client, enum g760a_regs reg,
77 u16 value)
78{
79 return i2c_smbus_write_byte_data(client, reg, value);
80}
81
Guenter Roeck1b9c4912012-01-19 11:02:18 -080082/*
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070083 * sysfs attributes
84 */
85
86static struct g760a_data *g760a_update_client(struct device *dev)
87{
Axel Line47c39b2014-07-01 10:57:38 +080088 struct g760a_data *data = dev_get_drvdata(dev);
89 struct i2c_client *client = data->client;
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -070090
91 mutex_lock(&data->update_lock);
92
93 if (time_after(jiffies, data->last_updated + G760A_UPDATE_INTERVAL)
94 || !data->valid) {
95 dev_dbg(&client->dev, "Starting g760a update\n");
96
97 data->set_cnt = g760a_read_value(client, G760A_REG_SET_CNT);
98 data->act_cnt = g760a_read_value(client, G760A_REG_ACT_CNT);
99 data->fan_sta = g760a_read_value(client, G760A_REG_FAN_STA);
100
101 data->last_updated = jiffies;
102 data->valid = 1;
103 }
104
105 mutex_unlock(&data->update_lock);
106
107 return data;
108}
109
110static ssize_t show_fan(struct device *dev, struct device_attribute *da,
111 char *buf)
112{
113 struct g760a_data *data = g760a_update_client(dev);
114 unsigned int rpm = 0;
115
116 mutex_lock(&data->update_lock);
117 if (!(data->fan_sta & G760A_REG_FAN_STA_RPM_LOW))
118 rpm = rpm_from_cnt(data->act_cnt, data->clk, data->fan_div);
119 mutex_unlock(&data->update_lock);
120
121 return sprintf(buf, "%d\n", rpm);
122}
123
124static ssize_t show_fan_alarm(struct device *dev, struct device_attribute *da,
125 char *buf)
126{
127 struct g760a_data *data = g760a_update_client(dev);
128
129 int fan_alarm = (data->fan_sta & G760A_REG_FAN_STA_RPM_OFF) ? 1 : 0;
130
131 return sprintf(buf, "%d\n", fan_alarm);
132}
133
134static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
135 char *buf)
136{
137 struct g760a_data *data = g760a_update_client(dev);
138
139 return sprintf(buf, "%d\n", PWM_FROM_CNT(data->set_cnt));
140}
141
142static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
143 const char *buf, size_t count)
144{
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700145 struct g760a_data *data = g760a_update_client(dev);
Axel Line47c39b2014-07-01 10:57:38 +0800146 struct i2c_client *client = data->client;
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700147 unsigned long val;
148
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100149 if (kstrtoul(buf, 10, &val))
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700150 return -EINVAL;
151
152 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800153 data->set_cnt = PWM_TO_CNT(clamp_val(val, 0, 255));
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700154 g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
155 mutex_unlock(&data->update_lock);
156
157 return count;
158}
159
160static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
161static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
162static DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL);
163
Axel Line47c39b2014-07-01 10:57:38 +0800164static struct attribute *g760a_attrs[] = {
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700165 &dev_attr_pwm1.attr,
166 &dev_attr_fan1_input.attr,
167 &dev_attr_fan1_alarm.attr,
168 NULL
169};
170
Axel Line47c39b2014-07-01 10:57:38 +0800171ATTRIBUTE_GROUPS(g760a);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700172
Guenter Roeck1b9c4912012-01-19 11:02:18 -0800173/*
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700174 * new-style driver model code
175 */
176
177static int g760a_probe(struct i2c_client *client,
178 const struct i2c_device_id *id)
179{
Axel Line47c39b2014-07-01 10:57:38 +0800180 struct device *dev = &client->dev;
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700181 struct g760a_data *data;
Axel Line47c39b2014-07-01 10:57:38 +0800182 struct device *hwmon_dev;
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700183
Axel Line47c39b2014-07-01 10:57:38 +0800184 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700185 return -EIO;
186
Axel Line47c39b2014-07-01 10:57:38 +0800187 data = devm_kzalloc(dev, sizeof(struct g760a_data), GFP_KERNEL);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700188 if (!data)
189 return -ENOMEM;
190
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700191 data->client = client;
192 mutex_init(&data->update_lock);
193
194 /* setup default configuration for now */
195 data->fan_div = G760A_DEFAULT_FAN_DIV;
196 data->clk = G760A_DEFAULT_CLK;
197
Axel Line47c39b2014-07-01 10:57:38 +0800198 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
199 data,
200 g760a_groups);
201 return PTR_ERR_OR_ZERO(hwmon_dev);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700202}
203
Axel Lin15ac4dd2014-07-01 10:55:55 +0800204static const struct i2c_device_id g760a_id[] = {
205 { "g760a", 0 },
206 { }
207};
208MODULE_DEVICE_TABLE(i2c, g760a_id);
209
210static struct i2c_driver g760a_driver = {
211 .driver = {
212 .name = "g760a",
213 },
214 .probe = g760a_probe,
Axel Lin15ac4dd2014-07-01 10:55:55 +0800215 .id_table = g760a_id,
216};
217
Axel Linf0967ee2012-01-20 15:38:18 +0800218module_i2c_driver(g760a_driver);
Herbert Valerio Riedel3bcfa9e2009-04-06 19:00:30 -0700219
220MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
221MODULE_DESCRIPTION("GMT G760A driver");
222MODULE_LICENSE("GPL");