Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Driver for +/-1 degree C, SMBus-Compatible Remote/Local Temperature Sensor |
| 4 | * with Overtemperature Alarm |
| 5 | * |
| 6 | * Copyright (C) 2011 AppearTV AS |
| 7 | * |
| 8 | * Derived from: |
| 9 | * |
| 10 | * Based on the max1619 driver. |
Oleksij Rempel | 9292f05 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 11 | * Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net> |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 12 | * Jean Delvare <jdelvare@suse.de> |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 13 | * |
| 14 | * The MAX6642 is a sensor chip made by Maxim. |
| 15 | * It reports up to two temperatures (its own plus up to |
| 16 | * one external one). Complete datasheet can be |
| 17 | * obtained from Maxim's website at: |
| 18 | * http://datasheets.maxim-ic.com/en/ds/MAX6642.pdf |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <linux/sysfs.h> |
| 32 | |
| 33 | static const unsigned short normal_i2c[] = { |
| 34 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
| 35 | |
| 36 | /* |
| 37 | * The MAX6642 registers |
| 38 | */ |
| 39 | |
| 40 | #define MAX6642_REG_R_MAN_ID 0xFE |
| 41 | #define MAX6642_REG_R_CONFIG 0x03 |
| 42 | #define MAX6642_REG_W_CONFIG 0x09 |
| 43 | #define MAX6642_REG_R_STATUS 0x02 |
| 44 | #define MAX6642_REG_R_LOCAL_TEMP 0x00 |
| 45 | #define MAX6642_REG_R_LOCAL_TEMPL 0x11 |
| 46 | #define MAX6642_REG_R_LOCAL_HIGH 0x05 |
| 47 | #define MAX6642_REG_W_LOCAL_HIGH 0x0B |
| 48 | #define MAX6642_REG_R_REMOTE_TEMP 0x01 |
| 49 | #define MAX6642_REG_R_REMOTE_TEMPL 0x10 |
| 50 | #define MAX6642_REG_R_REMOTE_HIGH 0x07 |
| 51 | #define MAX6642_REG_W_REMOTE_HIGH 0x0D |
| 52 | |
| 53 | /* |
| 54 | * Conversions |
| 55 | */ |
| 56 | |
| 57 | static int temp_from_reg10(int val) |
| 58 | { |
| 59 | return val * 250; |
| 60 | } |
| 61 | |
| 62 | static int temp_from_reg(int val) |
| 63 | { |
| 64 | return val * 1000; |
| 65 | } |
| 66 | |
| 67 | static int temp_to_reg(int val) |
| 68 | { |
| 69 | return val / 1000; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Client data (each client gets its own) |
| 74 | */ |
| 75 | |
| 76 | struct max6642_data { |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 77 | struct i2c_client *client; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 78 | struct mutex update_lock; |
| 79 | bool valid; /* zero until following fields are valid */ |
| 80 | unsigned long last_updated; /* in jiffies */ |
| 81 | |
| 82 | /* registers values */ |
| 83 | u16 temp_input[2]; /* local/remote */ |
| 84 | u16 temp_high[2]; /* local/remote */ |
| 85 | u8 alarms; |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * Real code |
| 90 | */ |
| 91 | |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 92 | static void max6642_init_client(struct max6642_data *data, |
| 93 | struct i2c_client *client) |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 94 | { |
| 95 | u8 config; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Start the conversions. |
| 99 | */ |
| 100 | config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG); |
| 101 | if (config & 0x40) |
| 102 | i2c_smbus_write_byte_data(client, MAX6642_REG_W_CONFIG, |
| 103 | config & 0xBF); /* run */ |
| 104 | |
| 105 | data->temp_high[0] = i2c_smbus_read_byte_data(client, |
| 106 | MAX6642_REG_R_LOCAL_HIGH); |
| 107 | data->temp_high[1] = i2c_smbus_read_byte_data(client, |
| 108 | MAX6642_REG_R_REMOTE_HIGH); |
| 109 | } |
| 110 | |
| 111 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 112 | static int max6642_detect(struct i2c_client *client, |
| 113 | struct i2c_board_info *info) |
| 114 | { |
| 115 | struct i2c_adapter *adapter = client->adapter; |
| 116 | u8 reg_config, reg_status, man_id; |
| 117 | |
| 118 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 119 | return -ENODEV; |
| 120 | |
| 121 | /* identification */ |
| 122 | man_id = i2c_smbus_read_byte_data(client, MAX6642_REG_R_MAN_ID); |
| 123 | if (man_id != 0x4D) |
| 124 | return -ENODEV; |
| 125 | |
Per Dalén | 942c1a9 | 2011-05-26 09:08:53 -0400 | [diff] [blame] | 126 | /* sanity check */ |
| 127 | if (i2c_smbus_read_byte_data(client, 0x04) != 0x4D |
| 128 | || i2c_smbus_read_byte_data(client, 0x06) != 0x4D |
| 129 | || i2c_smbus_read_byte_data(client, 0xff) != 0x4D) |
| 130 | return -ENODEV; |
| 131 | |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 132 | /* |
| 133 | * We read the config and status register, the 4 lower bits in the |
| 134 | * config register should be zero and bit 5, 3, 1 and 0 should be |
| 135 | * zero in the status register. |
| 136 | */ |
| 137 | reg_config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG); |
Per Dalén | 942c1a9 | 2011-05-26 09:08:53 -0400 | [diff] [blame] | 138 | if ((reg_config & 0x0f) != 0x00) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | /* in between, another round of sanity checks */ |
| 142 | if (i2c_smbus_read_byte_data(client, 0x04) != reg_config |
| 143 | || i2c_smbus_read_byte_data(client, 0x06) != reg_config |
| 144 | || i2c_smbus_read_byte_data(client, 0xff) != reg_config) |
| 145 | return -ENODEV; |
| 146 | |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 147 | reg_status = i2c_smbus_read_byte_data(client, MAX6642_REG_R_STATUS); |
Per Dalén | 942c1a9 | 2011-05-26 09:08:53 -0400 | [diff] [blame] | 148 | if ((reg_status & 0x2b) != 0x00) |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 149 | return -ENODEV; |
| 150 | |
| 151 | strlcpy(info->type, "max6642", I2C_NAME_SIZE); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static struct max6642_data *max6642_update_device(struct device *dev) |
| 157 | { |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 158 | struct max6642_data *data = dev_get_drvdata(dev); |
| 159 | struct i2c_client *client = data->client; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 160 | u16 val, tmp; |
| 161 | |
| 162 | mutex_lock(&data->update_lock); |
| 163 | |
| 164 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 165 | dev_dbg(dev, "Updating max6642 data.\n"); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 166 | val = i2c_smbus_read_byte_data(client, |
| 167 | MAX6642_REG_R_LOCAL_TEMPL); |
| 168 | tmp = (val >> 6) & 3; |
| 169 | val = i2c_smbus_read_byte_data(client, |
| 170 | MAX6642_REG_R_LOCAL_TEMP); |
| 171 | val = (val << 2) | tmp; |
| 172 | data->temp_input[0] = val; |
| 173 | val = i2c_smbus_read_byte_data(client, |
| 174 | MAX6642_REG_R_REMOTE_TEMPL); |
| 175 | tmp = (val >> 6) & 3; |
| 176 | val = i2c_smbus_read_byte_data(client, |
| 177 | MAX6642_REG_R_REMOTE_TEMP); |
| 178 | val = (val << 2) | tmp; |
| 179 | data->temp_input[1] = val; |
| 180 | data->alarms = i2c_smbus_read_byte_data(client, |
| 181 | MAX6642_REG_R_STATUS); |
| 182 | |
| 183 | data->last_updated = jiffies; |
| 184 | data->valid = 1; |
| 185 | } |
| 186 | |
| 187 | mutex_unlock(&data->update_lock); |
| 188 | |
| 189 | return data; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Sysfs stuff |
| 194 | */ |
| 195 | |
Guenter Roeck | 823b867 | 2018-12-10 14:02:17 -0800 | [diff] [blame] | 196 | static ssize_t temp_max10_show(struct device *dev, |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 197 | struct device_attribute *dev_attr, char *buf) |
| 198 | { |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 199 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 200 | struct max6642_data *data = max6642_update_device(dev); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 201 | |
| 202 | return sprintf(buf, "%d\n", |
| 203 | temp_from_reg10(data->temp_input[attr->index])); |
| 204 | } |
| 205 | |
Guenter Roeck | 823b867 | 2018-12-10 14:02:17 -0800 | [diff] [blame] | 206 | static ssize_t temp_max_show(struct device *dev, |
| 207 | struct device_attribute *attr, char *buf) |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 208 | { |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 209 | struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 210 | struct max6642_data *data = max6642_update_device(dev); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 211 | |
| 212 | return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr])); |
| 213 | } |
| 214 | |
Guenter Roeck | 823b867 | 2018-12-10 14:02:17 -0800 | [diff] [blame] | 215 | static ssize_t temp_max_store(struct device *dev, |
| 216 | struct device_attribute *attr, const char *buf, |
| 217 | size_t count) |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 218 | { |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 219 | struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); |
| 220 | struct max6642_data *data = dev_get_drvdata(dev); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 221 | unsigned long val; |
| 222 | int err; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 223 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 224 | err = kstrtoul(buf, 10, &val); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 225 | if (err < 0) |
| 226 | return err; |
| 227 | |
| 228 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 229 | data->temp_high[attr2->nr] = clamp_val(temp_to_reg(val), 0, 255); |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 230 | i2c_smbus_write_byte_data(data->client, attr2->index, |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 231 | data->temp_high[attr2->nr]); |
| 232 | mutex_unlock(&data->update_lock); |
| 233 | return count; |
| 234 | } |
| 235 | |
Guenter Roeck | 823b867 | 2018-12-10 14:02:17 -0800 | [diff] [blame] | 236 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 237 | char *buf) |
| 238 | { |
| 239 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 240 | struct max6642_data *data = max6642_update_device(dev); |
| 241 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); |
| 242 | } |
| 243 | |
Guenter Roeck | 823b867 | 2018-12-10 14:02:17 -0800 | [diff] [blame] | 244 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_max10, 0); |
| 245 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_max10, 1); |
| 246 | static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp_max, 0, |
| 247 | MAX6642_REG_W_LOCAL_HIGH); |
| 248 | static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp_max, 1, |
| 249 | MAX6642_REG_W_REMOTE_HIGH); |
| 250 | static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2); |
| 251 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6); |
| 252 | static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 253 | |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 254 | static struct attribute *max6642_attrs[] = { |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 255 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 256 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 257 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 258 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 259 | |
Per Dalen | 614198b | 2011-05-31 06:54:21 -0700 | [diff] [blame] | 260 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 261 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 262 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 263 | NULL |
| 264 | }; |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 265 | ATTRIBUTE_GROUPS(max6642); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 266 | |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 267 | static int max6642_probe(struct i2c_client *client, |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 268 | const struct i2c_device_id *id) |
| 269 | { |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 270 | struct device *dev = &client->dev; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 271 | struct max6642_data *data; |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 272 | struct device *hwmon_dev; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 273 | |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 274 | data = devm_kzalloc(dev, sizeof(struct max6642_data), GFP_KERNEL); |
Guenter Roeck | 96f316c | 2012-06-02 09:58:13 -0700 | [diff] [blame] | 275 | if (!data) |
| 276 | return -ENOMEM; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 277 | |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 278 | data->client = client; |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 279 | mutex_init(&data->update_lock); |
| 280 | |
| 281 | /* Initialize the MAX6642 chip */ |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 282 | max6642_init_client(data, client); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 283 | |
Guenter Roeck | 9bd024e | 2013-09-01 16:48:48 -0700 | [diff] [blame] | 284 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
| 285 | client->name, data, |
| 286 | max6642_groups); |
Fengguang Wu | e5840c7 | 2013-09-17 20:54:11 -0700 | [diff] [blame] | 287 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Driver data (common to all clients) |
| 292 | */ |
| 293 | |
| 294 | static const struct i2c_device_id max6642_id[] = { |
| 295 | { "max6642", 0 }, |
| 296 | { } |
| 297 | }; |
| 298 | MODULE_DEVICE_TABLE(i2c, max6642_id); |
| 299 | |
| 300 | static struct i2c_driver max6642_driver = { |
| 301 | .class = I2C_CLASS_HWMON, |
| 302 | .driver = { |
| 303 | .name = "max6642", |
| 304 | }, |
| 305 | .probe = max6642_probe, |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 306 | .id_table = max6642_id, |
| 307 | .detect = max6642_detect, |
| 308 | .address_list = normal_i2c, |
| 309 | }; |
| 310 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 311 | module_i2c_driver(max6642_driver); |
Per Dalén | 83bffbc | 2011-04-06 20:29:44 +0200 | [diff] [blame] | 312 | |
| 313 | MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>"); |
| 314 | MODULE_DESCRIPTION("MAX6642 sensor driver"); |
| 315 | MODULE_LICENSE("GPL"); |