1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2195a4b42SIl Han /*
3195a4b42SIl Han * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
4195a4b42SIl Han * monitoring.
5195a4b42SIl Han *
6195a4b42SIl Han * (C) 2015 by Il Han <corone.il.han@gmail.com>
7195a4b42SIl Han */
8195a4b42SIl Han
9195a4b42SIl Han #include <linux/err.h>
10195a4b42SIl Han #include <linux/hwmon.h>
11195a4b42SIl Han #include <linux/i2c.h>
12195a4b42SIl Han #include <linux/init.h>
13195a4b42SIl Han #include <linux/jiffies.h>
14195a4b42SIl Han #include <linux/module.h>
15195a4b42SIl Han #include <linux/slab.h>
16195a4b42SIl Han
17195a4b42SIl Han /* MAX31790 registers */
18195a4b42SIl Han #define MAX31790_REG_GLOBAL_CONFIG 0x00
19195a4b42SIl Han #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch))
20195a4b42SIl Han #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch))
21195a4b42SIl Han #define MAX31790_REG_FAN_FAULT_STATUS2 0x10
22195a4b42SIl Han #define MAX31790_REG_FAN_FAULT_STATUS1 0x11
23195a4b42SIl Han #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2)
24195a4b42SIl Han #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2)
25195a4b42SIl Han #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2)
26195a4b42SIl Han #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2)
27195a4b42SIl Han
28195a4b42SIl Han /* Fan Config register bits */
29195a4b42SIl Han #define MAX31790_FAN_CFG_RPM_MODE 0x80
30148c847cSGuenter Roeck #define MAX31790_FAN_CFG_CTRL_MON 0x10
31195a4b42SIl Han #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08
32195a4b42SIl Han #define MAX31790_FAN_CFG_TACH_INPUT 0x01
33195a4b42SIl Han
34195a4b42SIl Han /* Fan Dynamics register bits */
35195a4b42SIl Han #define MAX31790_FAN_DYN_SR_SHIFT 5
36195a4b42SIl Han #define MAX31790_FAN_DYN_SR_MASK 0xE0
37195a4b42SIl Han #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \
38195a4b42SIl Han >> MAX31790_FAN_DYN_SR_SHIFT)
39195a4b42SIl Han
40195a4b42SIl Han #define FAN_RPM_MIN 120
41195a4b42SIl Han #define FAN_RPM_MAX 7864320
42195a4b42SIl Han
431814c4e8SGuenter Roeck #define FAN_COUNT_REG_MAX 0xffe0
441814c4e8SGuenter Roeck
45195a4b42SIl Han #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \
46195a4b42SIl Han ((60 * (sr) * 8192) / ((reg) >> 4)) : \
47195a4b42SIl Han FAN_RPM_MAX)
48195a4b42SIl Han #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2))
49195a4b42SIl Han
50195a4b42SIl Han #define NR_CHANNEL 6
51195a4b42SIl Han
52195a4b42SIl Han /*
53195a4b42SIl Han * Client data (each client gets its own)
54195a4b42SIl Han */
55195a4b42SIl Han struct max31790_data {
56195a4b42SIl Han struct i2c_client *client;
57195a4b42SIl Han struct mutex update_lock;
58195a4b42SIl Han bool valid; /* zero until following fields are valid */
59195a4b42SIl Han unsigned long last_updated; /* in jiffies */
60195a4b42SIl Han
61195a4b42SIl Han /* register values */
62195a4b42SIl Han u8 fan_config[NR_CHANNEL];
63195a4b42SIl Han u8 fan_dynamics[NR_CHANNEL];
64195a4b42SIl Han u16 fault_status;
65195a4b42SIl Han u16 tach[NR_CHANNEL * 2];
66195a4b42SIl Han u16 pwm[NR_CHANNEL];
67195a4b42SIl Han u16 target_count[NR_CHANNEL];
68195a4b42SIl Han };
69195a4b42SIl Han
max31790_update_device(struct device * dev)70195a4b42SIl Han static struct max31790_data *max31790_update_device(struct device *dev)
71195a4b42SIl Han {
72195a4b42SIl Han struct max31790_data *data = dev_get_drvdata(dev);
73195a4b42SIl Han struct i2c_client *client = data->client;
74195a4b42SIl Han struct max31790_data *ret = data;
75195a4b42SIl Han int i;
76195a4b42SIl Han int rv;
77195a4b42SIl Han
78195a4b42SIl Han mutex_lock(&data->update_lock);
79195a4b42SIl Han
80195a4b42SIl Han if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
81195a4b42SIl Han rv = i2c_smbus_read_byte_data(client,
82195a4b42SIl Han MAX31790_REG_FAN_FAULT_STATUS1);
83195a4b42SIl Han if (rv < 0)
84195a4b42SIl Han goto abort;
852013607bSGuenter Roeck data->fault_status |= rv & 0x3F;
86195a4b42SIl Han
87195a4b42SIl Han rv = i2c_smbus_read_byte_data(client,
88195a4b42SIl Han MAX31790_REG_FAN_FAULT_STATUS2);
89195a4b42SIl Han if (rv < 0)
90195a4b42SIl Han goto abort;
91195a4b42SIl Han data->fault_status |= (rv & 0x3F) << 6;
92195a4b42SIl Han
93195a4b42SIl Han for (i = 0; i < NR_CHANNEL; i++) {
94195a4b42SIl Han rv = i2c_smbus_read_word_swapped(client,
95195a4b42SIl Han MAX31790_REG_TACH_COUNT(i));
96195a4b42SIl Han if (rv < 0)
97195a4b42SIl Han goto abort;
98195a4b42SIl Han data->tach[i] = rv;
99195a4b42SIl Han
100195a4b42SIl Han if (data->fan_config[i]
101195a4b42SIl Han & MAX31790_FAN_CFG_TACH_INPUT) {
102195a4b42SIl Han rv = i2c_smbus_read_word_swapped(client,
103195a4b42SIl Han MAX31790_REG_TACH_COUNT(NR_CHANNEL
104195a4b42SIl Han + i));
105195a4b42SIl Han if (rv < 0)
106195a4b42SIl Han goto abort;
107195a4b42SIl Han data->tach[NR_CHANNEL + i] = rv;
108195a4b42SIl Han } else {
109195a4b42SIl Han rv = i2c_smbus_read_word_swapped(client,
110897f6339SGuenter Roeck MAX31790_REG_PWM_DUTY_CYCLE(i));
111195a4b42SIl Han if (rv < 0)
112195a4b42SIl Han goto abort;
113195a4b42SIl Han data->pwm[i] = rv;
114195a4b42SIl Han
115195a4b42SIl Han rv = i2c_smbus_read_word_swapped(client,
116195a4b42SIl Han MAX31790_REG_TARGET_COUNT(i));
117195a4b42SIl Han if (rv < 0)
118195a4b42SIl Han goto abort;
119195a4b42SIl Han data->target_count[i] = rv;
120195a4b42SIl Han }
121195a4b42SIl Han }
122195a4b42SIl Han
123195a4b42SIl Han data->last_updated = jiffies;
124195a4b42SIl Han data->valid = true;
125195a4b42SIl Han }
126195a4b42SIl Han goto done;
127195a4b42SIl Han
128195a4b42SIl Han abort:
129195a4b42SIl Han data->valid = false;
130195a4b42SIl Han ret = ERR_PTR(rv);
131195a4b42SIl Han
132195a4b42SIl Han done:
133195a4b42SIl Han mutex_unlock(&data->update_lock);
134195a4b42SIl Han
135195a4b42SIl Han return ret;
136195a4b42SIl Han }
137195a4b42SIl Han
138195a4b42SIl Han static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
139195a4b42SIl Han
get_tach_period(u8 fan_dynamics)140195a4b42SIl Han static u8 get_tach_period(u8 fan_dynamics)
141195a4b42SIl Han {
142195a4b42SIl Han return tach_period[SR_FROM_REG(fan_dynamics)];
143195a4b42SIl Han }
144195a4b42SIl Han
bits_for_tach_period(int rpm)145195a4b42SIl Han static u8 bits_for_tach_period(int rpm)
146195a4b42SIl Han {
147195a4b42SIl Han u8 bits;
148195a4b42SIl Han
149195a4b42SIl Han if (rpm < 500)
150195a4b42SIl Han bits = 0x0;
151195a4b42SIl Han else if (rpm < 1000)
152195a4b42SIl Han bits = 0x1;
153195a4b42SIl Han else if (rpm < 2000)
154195a4b42SIl Han bits = 0x2;
155195a4b42SIl Han else if (rpm < 4000)
156195a4b42SIl Han bits = 0x3;
157195a4b42SIl Han else if (rpm < 8000)
158195a4b42SIl Han bits = 0x4;
159195a4b42SIl Han else
160195a4b42SIl Han bits = 0x5;
161195a4b42SIl Han
162195a4b42SIl Han return bits;
163195a4b42SIl Han }
164195a4b42SIl Han
max31790_read_fan(struct device * dev,u32 attr,int channel,long * val)16554187ff9SGuenter Roeck static int max31790_read_fan(struct device *dev, u32 attr, int channel,
16654187ff9SGuenter Roeck long *val)
167195a4b42SIl Han {
168195a4b42SIl Han struct max31790_data *data = max31790_update_device(dev);
169791432cfSSudip Mukherjee int sr, rpm;
170195a4b42SIl Han
171195a4b42SIl Han if (IS_ERR(data))
172195a4b42SIl Han return PTR_ERR(data);
173195a4b42SIl Han
17454187ff9SGuenter Roeck switch (attr) {
17554187ff9SGuenter Roeck case hwmon_fan_input:
176cbbf244fSGuenter Roeck sr = get_tach_period(data->fan_dynamics[channel % NR_CHANNEL]);
1771814c4e8SGuenter Roeck if (data->tach[channel] == FAN_COUNT_REG_MAX)
1781814c4e8SGuenter Roeck rpm = 0;
1791814c4e8SGuenter Roeck else
18054187ff9SGuenter Roeck rpm = RPM_FROM_REG(data->tach[channel], sr);
18154187ff9SGuenter Roeck *val = rpm;
18254187ff9SGuenter Roeck return 0;
18354187ff9SGuenter Roeck case hwmon_fan_target:
18454187ff9SGuenter Roeck sr = get_tach_period(data->fan_dynamics[channel]);
18554187ff9SGuenter Roeck rpm = RPM_FROM_REG(data->target_count[channel], sr);
18654187ff9SGuenter Roeck *val = rpm;
18754187ff9SGuenter Roeck return 0;
18854187ff9SGuenter Roeck case hwmon_fan_fault:
1892013607bSGuenter Roeck mutex_lock(&data->update_lock);
19054187ff9SGuenter Roeck *val = !!(data->fault_status & (1 << channel));
1912013607bSGuenter Roeck data->fault_status &= ~(1 << channel);
1922013607bSGuenter Roeck /*
1932013607bSGuenter Roeck * If a fault bit is set, we need to write into one of the fan
1942013607bSGuenter Roeck * configuration registers to clear it. Note that this also
1952013607bSGuenter Roeck * clears the fault for the companion channel if enabled.
1962013607bSGuenter Roeck */
1972013607bSGuenter Roeck if (*val) {
1982013607bSGuenter Roeck int reg = MAX31790_REG_TARGET_COUNT(channel % NR_CHANNEL);
1992013607bSGuenter Roeck
2002013607bSGuenter Roeck i2c_smbus_write_byte_data(data->client, reg,
2012013607bSGuenter Roeck data->target_count[channel % NR_CHANNEL] >> 8);
2022013607bSGuenter Roeck }
2032013607bSGuenter Roeck mutex_unlock(&data->update_lock);
20454187ff9SGuenter Roeck return 0;
2055b38279eSJustin Ledford case hwmon_fan_enable:
2065b38279eSJustin Ledford *val = !!(data->fan_config[channel] & MAX31790_FAN_CFG_TACH_INPUT_EN);
2075b38279eSJustin Ledford return 0;
20854187ff9SGuenter Roeck default:
20954187ff9SGuenter Roeck return -EOPNOTSUPP;
21054187ff9SGuenter Roeck }
211195a4b42SIl Han }
212195a4b42SIl Han
max31790_write_fan(struct device * dev,u32 attr,int channel,long val)21354187ff9SGuenter Roeck static int max31790_write_fan(struct device *dev, u32 attr, int channel,
21454187ff9SGuenter Roeck long val)
215195a4b42SIl Han {
216195a4b42SIl Han struct max31790_data *data = dev_get_drvdata(dev);
217195a4b42SIl Han struct i2c_client *client = data->client;
21854187ff9SGuenter Roeck int target_count;
21954187ff9SGuenter Roeck int err = 0;
2205b38279eSJustin Ledford u8 bits, fan_config;
221195a4b42SIl Han int sr;
222195a4b42SIl Han
223195a4b42SIl Han mutex_lock(&data->update_lock);
224195a4b42SIl Han
22554187ff9SGuenter Roeck switch (attr) {
22654187ff9SGuenter Roeck case hwmon_fan_target:
22754187ff9SGuenter Roeck val = clamp_val(val, FAN_RPM_MIN, FAN_RPM_MAX);
22854187ff9SGuenter Roeck bits = bits_for_tach_period(val);
22954187ff9SGuenter Roeck data->fan_dynamics[channel] =
23054187ff9SGuenter Roeck ((data->fan_dynamics[channel] &
23154187ff9SGuenter Roeck ~MAX31790_FAN_DYN_SR_MASK) |
23254187ff9SGuenter Roeck (bits << MAX31790_FAN_DYN_SR_SHIFT));
233195a4b42SIl Han err = i2c_smbus_write_byte_data(client,
23454187ff9SGuenter Roeck MAX31790_REG_FAN_DYNAMICS(channel),
23554187ff9SGuenter Roeck data->fan_dynamics[channel]);
23654187ff9SGuenter Roeck if (err < 0)
23754187ff9SGuenter Roeck break;
238195a4b42SIl Han
23954187ff9SGuenter Roeck sr = get_tach_period(data->fan_dynamics[channel]);
24054187ff9SGuenter Roeck target_count = RPM_TO_REG(val, sr);
241195a4b42SIl Han target_count = clamp_val(target_count, 0x1, 0x7FF);
242195a4b42SIl Han
24354187ff9SGuenter Roeck data->target_count[channel] = target_count << 5;
244195a4b42SIl Han
245195a4b42SIl Han err = i2c_smbus_write_word_swapped(client,
24654187ff9SGuenter Roeck MAX31790_REG_TARGET_COUNT(channel),
24754187ff9SGuenter Roeck data->target_count[channel]);
248195a4b42SIl Han break;
2495b38279eSJustin Ledford case hwmon_fan_enable:
2505b38279eSJustin Ledford fan_config = data->fan_config[channel];
2515b38279eSJustin Ledford if (val == 0) {
2525b38279eSJustin Ledford fan_config &= ~MAX31790_FAN_CFG_TACH_INPUT_EN;
2535b38279eSJustin Ledford } else if (val == 1) {
2545b38279eSJustin Ledford fan_config |= MAX31790_FAN_CFG_TACH_INPUT_EN;
2555b38279eSJustin Ledford } else {
2565b38279eSJustin Ledford err = -EINVAL;
2575b38279eSJustin Ledford break;
2585b38279eSJustin Ledford }
2595b38279eSJustin Ledford if (fan_config != data->fan_config[channel]) {
2605b38279eSJustin Ledford err = i2c_smbus_write_byte_data(client, MAX31790_REG_FAN_CONFIG(channel),
2615b38279eSJustin Ledford fan_config);
2625b38279eSJustin Ledford if (!err)
2635b38279eSJustin Ledford data->fan_config[channel] = fan_config;
2645b38279eSJustin Ledford }
2655b38279eSJustin Ledford break;
266195a4b42SIl Han default:
26754187ff9SGuenter Roeck err = -EOPNOTSUPP;
26854187ff9SGuenter Roeck break;
269195a4b42SIl Han }
270195a4b42SIl Han
271195a4b42SIl Han mutex_unlock(&data->update_lock);
272195a4b42SIl Han
273195a4b42SIl Han return err;
274195a4b42SIl Han }
275195a4b42SIl Han
max31790_fan_is_visible(const void * _data,u32 attr,int channel)27654187ff9SGuenter Roeck static umode_t max31790_fan_is_visible(const void *_data, u32 attr, int channel)
277195a4b42SIl Han {
27854187ff9SGuenter Roeck const struct max31790_data *data = _data;
27954187ff9SGuenter Roeck u8 fan_config = data->fan_config[channel % NR_CHANNEL];
28054187ff9SGuenter Roeck
28154187ff9SGuenter Roeck switch (attr) {
28254187ff9SGuenter Roeck case hwmon_fan_input:
28354187ff9SGuenter Roeck case hwmon_fan_fault:
28454187ff9SGuenter Roeck if (channel < NR_CHANNEL ||
28554187ff9SGuenter Roeck (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
286dc8dbb4dSGuenter Roeck return 0444;
28754187ff9SGuenter Roeck return 0;
28854187ff9SGuenter Roeck case hwmon_fan_target:
28954187ff9SGuenter Roeck if (channel < NR_CHANNEL &&
29054187ff9SGuenter Roeck !(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
291dc8dbb4dSGuenter Roeck return 0644;
29254187ff9SGuenter Roeck return 0;
2935b38279eSJustin Ledford case hwmon_fan_enable:
2945b38279eSJustin Ledford if (channel < NR_CHANNEL)
2955b38279eSJustin Ledford return 0644;
2965b38279eSJustin Ledford return 0;
29754187ff9SGuenter Roeck default:
29854187ff9SGuenter Roeck return 0;
29954187ff9SGuenter Roeck }
30054187ff9SGuenter Roeck }
30154187ff9SGuenter Roeck
max31790_read_pwm(struct device * dev,u32 attr,int channel,long * val)30254187ff9SGuenter Roeck static int max31790_read_pwm(struct device *dev, u32 attr, int channel,
30354187ff9SGuenter Roeck long *val)
30454187ff9SGuenter Roeck {
305195a4b42SIl Han struct max31790_data *data = max31790_update_device(dev);
30694cdc560SDan Carpenter u8 fan_config;
307195a4b42SIl Han
308195a4b42SIl Han if (IS_ERR(data))
309195a4b42SIl Han return PTR_ERR(data);
310195a4b42SIl Han
31194cdc560SDan Carpenter fan_config = data->fan_config[channel];
31294cdc560SDan Carpenter
31354187ff9SGuenter Roeck switch (attr) {
31454187ff9SGuenter Roeck case hwmon_pwm_input:
31554187ff9SGuenter Roeck *val = data->pwm[channel] >> 8;
31654187ff9SGuenter Roeck return 0;
31754187ff9SGuenter Roeck case hwmon_pwm_enable:
318148c847cSGuenter Roeck if (fan_config & MAX31790_FAN_CFG_CTRL_MON)
31954187ff9SGuenter Roeck *val = 0;
320148c847cSGuenter Roeck else if (fan_config & MAX31790_FAN_CFG_RPM_MODE)
321148c847cSGuenter Roeck *val = 2;
322148c847cSGuenter Roeck else
323148c847cSGuenter Roeck *val = 1;
32454187ff9SGuenter Roeck return 0;
32554187ff9SGuenter Roeck default:
32654187ff9SGuenter Roeck return -EOPNOTSUPP;
32754187ff9SGuenter Roeck }
328195a4b42SIl Han }
329195a4b42SIl Han
max31790_write_pwm(struct device * dev,u32 attr,int channel,long val)33054187ff9SGuenter Roeck static int max31790_write_pwm(struct device *dev, u32 attr, int channel,
33154187ff9SGuenter Roeck long val)
33254187ff9SGuenter Roeck {
33354187ff9SGuenter Roeck struct max31790_data *data = dev_get_drvdata(dev);
33454187ff9SGuenter Roeck struct i2c_client *client = data->client;
33554187ff9SGuenter Roeck u8 fan_config;
33654187ff9SGuenter Roeck int err = 0;
337195a4b42SIl Han
33854187ff9SGuenter Roeck mutex_lock(&data->update_lock);
339195a4b42SIl Han
34054187ff9SGuenter Roeck switch (attr) {
34154187ff9SGuenter Roeck case hwmon_pwm_input:
34254187ff9SGuenter Roeck if (val < 0 || val > 255) {
34354187ff9SGuenter Roeck err = -EINVAL;
34454187ff9SGuenter Roeck break;
34554187ff9SGuenter Roeck }
346897f6339SGuenter Roeck data->valid = false;
34754187ff9SGuenter Roeck err = i2c_smbus_write_word_swapped(client,
34854187ff9SGuenter Roeck MAX31790_REG_PWMOUT(channel),
349897f6339SGuenter Roeck val << 8);
35054187ff9SGuenter Roeck break;
35154187ff9SGuenter Roeck case hwmon_pwm_enable:
35254187ff9SGuenter Roeck fan_config = data->fan_config[channel];
35354187ff9SGuenter Roeck if (val == 0) {
354148c847cSGuenter Roeck fan_config |= MAX31790_FAN_CFG_CTRL_MON;
355148c847cSGuenter Roeck /*
356148c847cSGuenter Roeck * Disable RPM mode; otherwise disabling fan speed
357148c847cSGuenter Roeck * monitoring is not possible.
358148c847cSGuenter Roeck */
359148c847cSGuenter Roeck fan_config &= ~MAX31790_FAN_CFG_RPM_MODE;
36054187ff9SGuenter Roeck } else if (val == 1) {
361148c847cSGuenter Roeck fan_config &= ~(MAX31790_FAN_CFG_CTRL_MON | MAX31790_FAN_CFG_RPM_MODE);
36254187ff9SGuenter Roeck } else if (val == 2) {
363148c847cSGuenter Roeck fan_config &= ~MAX31790_FAN_CFG_CTRL_MON;
364148c847cSGuenter Roeck /*
365148c847cSGuenter Roeck * The chip sets MAX31790_FAN_CFG_TACH_INPUT_EN on its
366148c847cSGuenter Roeck * own if MAX31790_FAN_CFG_RPM_MODE is set.
367148c847cSGuenter Roeck * Do it here as well to reflect the actual register
368148c847cSGuenter Roeck * value in the cache.
369148c847cSGuenter Roeck */
370148c847cSGuenter Roeck fan_config |= (MAX31790_FAN_CFG_RPM_MODE | MAX31790_FAN_CFG_TACH_INPUT_EN);
37154187ff9SGuenter Roeck } else {
37254187ff9SGuenter Roeck err = -EINVAL;
37354187ff9SGuenter Roeck break;
37454187ff9SGuenter Roeck }
375148c847cSGuenter Roeck if (fan_config != data->fan_config[channel]) {
376148c847cSGuenter Roeck err = i2c_smbus_write_byte_data(client, MAX31790_REG_FAN_CONFIG(channel),
37754187ff9SGuenter Roeck fan_config);
378148c847cSGuenter Roeck if (!err)
379148c847cSGuenter Roeck data->fan_config[channel] = fan_config;
380148c847cSGuenter Roeck }
38154187ff9SGuenter Roeck break;
38254187ff9SGuenter Roeck default:
38354187ff9SGuenter Roeck err = -EOPNOTSUPP;
38454187ff9SGuenter Roeck break;
38554187ff9SGuenter Roeck }
386195a4b42SIl Han
38754187ff9SGuenter Roeck mutex_unlock(&data->update_lock);
388195a4b42SIl Han
38954187ff9SGuenter Roeck return err;
39054187ff9SGuenter Roeck }
391195a4b42SIl Han
max31790_pwm_is_visible(const void * _data,u32 attr,int channel)39254187ff9SGuenter Roeck static umode_t max31790_pwm_is_visible(const void *_data, u32 attr, int channel)
39354187ff9SGuenter Roeck {
39454187ff9SGuenter Roeck const struct max31790_data *data = _data;
39554187ff9SGuenter Roeck u8 fan_config = data->fan_config[channel];
396195a4b42SIl Han
39754187ff9SGuenter Roeck switch (attr) {
39854187ff9SGuenter Roeck case hwmon_pwm_input:
39954187ff9SGuenter Roeck case hwmon_pwm_enable:
40054187ff9SGuenter Roeck if (!(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
401dc8dbb4dSGuenter Roeck return 0644;
40254187ff9SGuenter Roeck return 0;
40354187ff9SGuenter Roeck default:
40454187ff9SGuenter Roeck return 0;
40554187ff9SGuenter Roeck }
40654187ff9SGuenter Roeck }
407195a4b42SIl Han
max31790_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)40854187ff9SGuenter Roeck static int max31790_read(struct device *dev, enum hwmon_sensor_types type,
40954187ff9SGuenter Roeck u32 attr, int channel, long *val)
41054187ff9SGuenter Roeck {
41154187ff9SGuenter Roeck switch (type) {
41254187ff9SGuenter Roeck case hwmon_fan:
41354187ff9SGuenter Roeck return max31790_read_fan(dev, attr, channel, val);
41454187ff9SGuenter Roeck case hwmon_pwm:
41554187ff9SGuenter Roeck return max31790_read_pwm(dev, attr, channel, val);
41654187ff9SGuenter Roeck default:
41754187ff9SGuenter Roeck return -EOPNOTSUPP;
41854187ff9SGuenter Roeck }
41954187ff9SGuenter Roeck }
420195a4b42SIl Han
max31790_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)42154187ff9SGuenter Roeck static int max31790_write(struct device *dev, enum hwmon_sensor_types type,
42254187ff9SGuenter Roeck u32 attr, int channel, long val)
42354187ff9SGuenter Roeck {
42454187ff9SGuenter Roeck switch (type) {
42554187ff9SGuenter Roeck case hwmon_fan:
42654187ff9SGuenter Roeck return max31790_write_fan(dev, attr, channel, val);
42754187ff9SGuenter Roeck case hwmon_pwm:
42854187ff9SGuenter Roeck return max31790_write_pwm(dev, attr, channel, val);
42954187ff9SGuenter Roeck default:
43054187ff9SGuenter Roeck return -EOPNOTSUPP;
43154187ff9SGuenter Roeck }
43254187ff9SGuenter Roeck }
433195a4b42SIl Han
max31790_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)43454187ff9SGuenter Roeck static umode_t max31790_is_visible(const void *data,
43554187ff9SGuenter Roeck enum hwmon_sensor_types type,
43654187ff9SGuenter Roeck u32 attr, int channel)
43754187ff9SGuenter Roeck {
43854187ff9SGuenter Roeck switch (type) {
43954187ff9SGuenter Roeck case hwmon_fan:
44054187ff9SGuenter Roeck return max31790_fan_is_visible(data, attr, channel);
44154187ff9SGuenter Roeck case hwmon_pwm:
44254187ff9SGuenter Roeck return max31790_pwm_is_visible(data, attr, channel);
44354187ff9SGuenter Roeck default:
44454187ff9SGuenter Roeck return 0;
44554187ff9SGuenter Roeck }
44654187ff9SGuenter Roeck }
447195a4b42SIl Han
448*822ce415SKrzysztof Kozlowski static const struct hwmon_channel_info * const max31790_info[] = {
449b605e671SGuenter Roeck HWMON_CHANNEL_INFO(fan,
4505b38279eSJustin Ledford HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT | HWMON_F_ENABLE,
4515b38279eSJustin Ledford HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT | HWMON_F_ENABLE,
4525b38279eSJustin Ledford HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT | HWMON_F_ENABLE,
4535b38279eSJustin Ledford HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT | HWMON_F_ENABLE,
4545b38279eSJustin Ledford HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT | HWMON_F_ENABLE,
4555b38279eSJustin Ledford HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT | HWMON_F_ENABLE,
456b605e671SGuenter Roeck HWMON_F_INPUT | HWMON_F_FAULT,
457b605e671SGuenter Roeck HWMON_F_INPUT | HWMON_F_FAULT,
458b605e671SGuenter Roeck HWMON_F_INPUT | HWMON_F_FAULT,
459b605e671SGuenter Roeck HWMON_F_INPUT | HWMON_F_FAULT,
460b605e671SGuenter Roeck HWMON_F_INPUT | HWMON_F_FAULT,
461b605e671SGuenter Roeck HWMON_F_INPUT | HWMON_F_FAULT),
462b605e671SGuenter Roeck HWMON_CHANNEL_INFO(pwm,
463b605e671SGuenter Roeck HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
464b605e671SGuenter Roeck HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
465b605e671SGuenter Roeck HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
466b605e671SGuenter Roeck HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
467b605e671SGuenter Roeck HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
468b605e671SGuenter Roeck HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
469195a4b42SIl Han NULL
470195a4b42SIl Han };
471195a4b42SIl Han
47254187ff9SGuenter Roeck static const struct hwmon_ops max31790_hwmon_ops = {
47354187ff9SGuenter Roeck .is_visible = max31790_is_visible,
47454187ff9SGuenter Roeck .read = max31790_read,
47554187ff9SGuenter Roeck .write = max31790_write,
476195a4b42SIl Han };
47754187ff9SGuenter Roeck
47854187ff9SGuenter Roeck static const struct hwmon_chip_info max31790_chip_info = {
47954187ff9SGuenter Roeck .ops = &max31790_hwmon_ops,
48054187ff9SGuenter Roeck .info = max31790_info,
48154187ff9SGuenter Roeck };
482195a4b42SIl Han
max31790_init_client(struct i2c_client * client,struct max31790_data * data)483195a4b42SIl Han static int max31790_init_client(struct i2c_client *client,
484195a4b42SIl Han struct max31790_data *data)
485195a4b42SIl Han {
486195a4b42SIl Han int i, rv;
487195a4b42SIl Han
488195a4b42SIl Han for (i = 0; i < NR_CHANNEL; i++) {
489195a4b42SIl Han rv = i2c_smbus_read_byte_data(client,
490195a4b42SIl Han MAX31790_REG_FAN_CONFIG(i));
491195a4b42SIl Han if (rv < 0)
492195a4b42SIl Han return rv;
493195a4b42SIl Han data->fan_config[i] = rv;
494195a4b42SIl Han
495195a4b42SIl Han rv = i2c_smbus_read_byte_data(client,
496195a4b42SIl Han MAX31790_REG_FAN_DYNAMICS(i));
497195a4b42SIl Han if (rv < 0)
498195a4b42SIl Han return rv;
499195a4b42SIl Han data->fan_dynamics[i] = rv;
500195a4b42SIl Han }
501195a4b42SIl Han
502195a4b42SIl Han return 0;
503195a4b42SIl Han }
504195a4b42SIl Han
max31790_probe(struct i2c_client * client)50567487038SStephen Kitt static int max31790_probe(struct i2c_client *client)
506195a4b42SIl Han {
507195a4b42SIl Han struct i2c_adapter *adapter = client->adapter;
508195a4b42SIl Han struct device *dev = &client->dev;
509195a4b42SIl Han struct max31790_data *data;
510195a4b42SIl Han struct device *hwmon_dev;
511195a4b42SIl Han int err;
512195a4b42SIl Han
513195a4b42SIl Han if (!i2c_check_functionality(adapter,
514195a4b42SIl Han I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
515195a4b42SIl Han return -ENODEV;
516195a4b42SIl Han
517195a4b42SIl Han data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
518195a4b42SIl Han if (!data)
519195a4b42SIl Han return -ENOMEM;
520195a4b42SIl Han
521195a4b42SIl Han data->client = client;
522195a4b42SIl Han mutex_init(&data->update_lock);
523195a4b42SIl Han
524195a4b42SIl Han /*
525195a4b42SIl Han * Initialize the max31790 chip
526195a4b42SIl Han */
527195a4b42SIl Han err = max31790_init_client(client, data);
528195a4b42SIl Han if (err)
529195a4b42SIl Han return err;
530195a4b42SIl Han
53154187ff9SGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
53254187ff9SGuenter Roeck data,
53354187ff9SGuenter Roeck &max31790_chip_info,
53454187ff9SGuenter Roeck NULL);
535195a4b42SIl Han
536195a4b42SIl Han return PTR_ERR_OR_ZERO(hwmon_dev);
537195a4b42SIl Han }
538195a4b42SIl Han
539195a4b42SIl Han static const struct i2c_device_id max31790_id[] = {
540195a4b42SIl Han { "max31790", 0 },
541195a4b42SIl Han { }
542195a4b42SIl Han };
543195a4b42SIl Han MODULE_DEVICE_TABLE(i2c, max31790_id);
544195a4b42SIl Han
545195a4b42SIl Han static struct i2c_driver max31790_driver = {
546195a4b42SIl Han .class = I2C_CLASS_HWMON,
54767487038SStephen Kitt .probe = max31790_probe,
548195a4b42SIl Han .driver = {
549195a4b42SIl Han .name = "max31790",
550195a4b42SIl Han },
551195a4b42SIl Han .id_table = max31790_id,
552195a4b42SIl Han };
553195a4b42SIl Han
554195a4b42SIl Han module_i2c_driver(max31790_driver);
555195a4b42SIl Han
556195a4b42SIl Han MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
557195a4b42SIl Han MODULE_DESCRIPTION("MAX31790 sensor driver");
558195a4b42SIl Han MODULE_LICENSE("GPL");
559