1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 David George <david.george@ska.ac.za>
6 * some credit to Christoph Scheurer, but largely a rewrite
9 #include <linux/bitops.h>
10 #include <linux/bits.h>
11 #include <linux/err.h>
12 #include <linux/hwmon.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
19 /* Addresses to scan */
20 static const unsigned short max1668_addr_list
[] = {
21 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
23 /* max1668 registers */
25 #define MAX1668_REG_TEMP(nr) (nr)
26 #define MAX1668_REG_STAT1 0x05
27 #define MAX1668_REG_STAT2 0x06
28 #define MAX1668_REG_MAN_ID 0xfe
29 #define MAX1668_REG_DEV_ID 0xff
34 #define MAX1668_REG_LIMH(nr) (0x08 + 2 * (nr))
36 #define MAX1668_REG_LIML(nr) (0x09 + 2 * (nr))
38 /* manufacturer and device ID Constants */
39 #define MAN_ID_MAXIM 0x4d
40 #define DEV_ID_MAX1668 0x3
41 #define DEV_ID_MAX1805 0x5
42 #define DEV_ID_MAX1989 0xb
44 /* read only mode module parameter */
45 static bool read_only
;
46 module_param(read_only
, bool, 0);
47 MODULE_PARM_DESC(read_only
, "Don't set any values, read only mode");
50 struct regmap
*regmap
;
54 static int max1668_read(struct device
*dev
, enum hwmon_sensor_types type
,
55 u32 attr
, int channel
, long *val
)
57 struct max1668_data
*data
= dev_get_drvdata(dev
);
58 struct regmap
*regmap
= data
->regmap
;
59 u32 regs
[2] = { MAX1668_REG_STAT1
, MAX1668_REG_TEMP(channel
) };
65 case hwmon_temp_input
:
66 ret
= regmap_read(regmap
, MAX1668_REG_TEMP(channel
), ®val
);
69 *val
= sign_extend32(regval
, 7) * 1000;
72 ret
= regmap_read(regmap
, MAX1668_REG_LIML(channel
), ®val
);
75 *val
= sign_extend32(regval
, 7) * 1000;
78 ret
= regmap_read(regmap
, MAX1668_REG_LIMH(channel
), ®val
);
81 *val
= sign_extend32(regval
, 7) * 1000;
83 case hwmon_temp_min_alarm
:
84 ret
= regmap_read(regmap
,
85 channel
? MAX1668_REG_STAT2
: MAX1668_REG_STAT1
,
90 *val
= !!(regval
& BIT(9 - channel
* 2));
92 *val
= !!(regval
& BIT(5));
94 case hwmon_temp_max_alarm
:
95 ret
= regmap_read(regmap
,
96 channel
? MAX1668_REG_STAT2
: MAX1668_REG_STAT1
,
101 *val
= !!(regval
& BIT(8 - channel
* 2));
103 *val
= !!(regval
& BIT(6));
105 case hwmon_temp_fault
:
106 ret
= regmap_multi_reg_read(regmap
, regs
, regvals
, 2);
109 *val
= !!((regvals
[0] & BIT(4)) && regvals
[1] == 127);
117 static int max1668_write(struct device
*dev
, enum hwmon_sensor_types type
,
118 u32 attr
, int channel
, long val
)
120 struct max1668_data
*data
= dev_get_drvdata(dev
);
121 struct regmap
*regmap
= data
->regmap
;
123 val
= clamp_val(val
/ 1000, -128, 127);
127 return regmap_write(regmap
, MAX1668_REG_LIML(channel
), val
);
129 return regmap_write(regmap
, MAX1668_REG_LIMH(channel
), val
);
135 static umode_t
max1668_is_visible(const void *_data
, enum hwmon_sensor_types type
,
136 u32 attr
, int channel
)
138 const struct max1668_data
*data
= _data
;
140 if (channel
>= data
->channels
)
146 return read_only
? 0444 : 0644;
147 case hwmon_temp_input
:
148 case hwmon_temp_min_alarm
:
149 case hwmon_temp_max_alarm
:
151 case hwmon_temp_fault
:
161 static const struct hwmon_channel_info
* const max1668_info
[] = {
162 HWMON_CHANNEL_INFO(temp
,
163 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
164 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
,
165 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
166 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
168 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
169 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
171 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
172 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
174 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
175 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
180 static const struct hwmon_ops max1668_hwmon_ops
= {
181 .is_visible
= max1668_is_visible
,
182 .read
= max1668_read
,
183 .write
= max1668_write
,
186 static const struct hwmon_chip_info max1668_chip_info
= {
187 .ops
= &max1668_hwmon_ops
,
188 .info
= max1668_info
,
191 /* Return 0 if detection is successful, -ENODEV otherwise */
192 static int max1668_detect(struct i2c_client
*client
,
193 struct i2c_board_info
*info
)
195 struct i2c_adapter
*adapter
= client
->adapter
;
196 const char *type_name
;
199 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
202 /* Check for unsupported part */
203 man_id
= i2c_smbus_read_byte_data(client
, MAX1668_REG_MAN_ID
);
204 if (man_id
!= MAN_ID_MAXIM
)
207 dev_id
= i2c_smbus_read_byte_data(client
, MAX1668_REG_DEV_ID
);
212 if (dev_id
== DEV_ID_MAX1668
)
213 type_name
= "max1668";
214 else if (dev_id
== DEV_ID_MAX1805
)
215 type_name
= "max1805";
216 else if (dev_id
== DEV_ID_MAX1989
)
217 type_name
= "max1989";
222 strscpy(info
->type
, type_name
, I2C_NAME_SIZE
);
229 static int max1668_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
233 ret
= i2c_smbus_read_byte_data(context
, reg
);
241 static int max1668_reg_write(void *context
, unsigned int reg
, unsigned int val
)
243 return i2c_smbus_write_byte_data(context
, reg
+ 11, val
);
246 static bool max1668_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
248 return reg
<= MAX1668_REG_STAT2
;
251 static bool max1668_regmap_is_writeable(struct device
*dev
, unsigned int reg
)
253 return reg
> MAX1668_REG_STAT2
&& reg
<= MAX1668_REG_LIML(4);
256 static const struct regmap_config max1668_regmap_config
= {
259 .cache_type
= REGCACHE_MAPLE
,
260 .volatile_reg
= max1668_regmap_is_volatile
,
261 .writeable_reg
= max1668_regmap_is_writeable
,
264 static const struct regmap_bus max1668_regmap_bus
= {
265 .reg_write
= max1668_reg_write
,
266 .reg_read
= max1668_reg_read
,
269 static int max1668_probe(struct i2c_client
*client
)
271 struct i2c_adapter
*adapter
= client
->adapter
;
272 struct device
*dev
= &client
->dev
;
273 struct device
*hwmon_dev
;
274 struct max1668_data
*data
;
276 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
279 data
= devm_kzalloc(dev
, sizeof(struct max1668_data
), GFP_KERNEL
);
283 data
->regmap
= devm_regmap_init(dev
, &max1668_regmap_bus
, client
,
284 &max1668_regmap_config
);
285 if (IS_ERR(data
->regmap
))
286 return PTR_ERR(data
->regmap
);
288 data
->channels
= (uintptr_t)i2c_get_match_data(client
);
290 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
, data
,
291 &max1668_chip_info
, NULL
);
292 return PTR_ERR_OR_ZERO(hwmon_dev
);
295 static const struct i2c_device_id max1668_id
[] = {
301 MODULE_DEVICE_TABLE(i2c
, max1668_id
);
303 /* This is the driver that will be inserted */
304 static struct i2c_driver max1668_driver
= {
305 .class = I2C_CLASS_HWMON
,
309 .probe
= max1668_probe
,
310 .id_table
= max1668_id
,
311 .detect
= max1668_detect
,
312 .address_list
= max1668_addr_list
,
315 module_i2c_driver(max1668_driver
);
317 MODULE_AUTHOR("David George <david.george@ska.ac.za>");
318 MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
319 MODULE_LICENSE("GPL");