1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28d5d45fbSJean Delvare /*
38d5d45fbSJean Delvare * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
48d5d45fbSJean Delvare * monitoring
59292f055SOleksij Rempel * Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
67c81c60fSJean Delvare * Jean Delvare <jdelvare@suse.de>
78d5d45fbSJean Delvare *
88d5d45fbSJean Delvare * Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
98d5d45fbSJean Delvare * It reports up to two temperatures (its own plus up to
108d5d45fbSJean Delvare * one external one). Complete datasheet can be
118d5d45fbSJean Delvare * obtained from Maxim's website at:
128d5d45fbSJean Delvare * http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
138d5d45fbSJean Delvare */
148d5d45fbSJean Delvare
158d5d45fbSJean Delvare #include <linux/module.h>
168d5d45fbSJean Delvare #include <linux/init.h>
178d5d45fbSJean Delvare #include <linux/slab.h>
188d5d45fbSJean Delvare #include <linux/jiffies.h>
198d5d45fbSJean Delvare #include <linux/i2c.h>
20943b0830SMark M. Hoffman #include <linux/hwmon.h>
2171062ffcSJean Delvare #include <linux/hwmon-sysfs.h>
22943b0830SMark M. Hoffman #include <linux/err.h>
239a61bf63SIngo Molnar #include <linux/mutex.h>
24a5ebe668SJean Delvare #include <linux/sysfs.h>
258d5d45fbSJean Delvare
2625e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = {
2725e9c86dSMark M. Hoffman 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
288d5d45fbSJean Delvare
298d5d45fbSJean Delvare /*
308d5d45fbSJean Delvare * The MAX1619 registers
318d5d45fbSJean Delvare */
328d5d45fbSJean Delvare
338d5d45fbSJean Delvare #define MAX1619_REG_R_MAN_ID 0xFE
348d5d45fbSJean Delvare #define MAX1619_REG_R_CHIP_ID 0xFF
358d5d45fbSJean Delvare #define MAX1619_REG_R_CONFIG 0x03
368d5d45fbSJean Delvare #define MAX1619_REG_W_CONFIG 0x09
378d5d45fbSJean Delvare #define MAX1619_REG_R_CONVRATE 0x04
388d5d45fbSJean Delvare #define MAX1619_REG_W_CONVRATE 0x0A
398d5d45fbSJean Delvare #define MAX1619_REG_R_STATUS 0x02
408d5d45fbSJean Delvare #define MAX1619_REG_R_LOCAL_TEMP 0x00
418d5d45fbSJean Delvare #define MAX1619_REG_R_REMOTE_TEMP 0x01
428d5d45fbSJean Delvare #define MAX1619_REG_R_REMOTE_HIGH 0x07
438d5d45fbSJean Delvare #define MAX1619_REG_W_REMOTE_HIGH 0x0D
448d5d45fbSJean Delvare #define MAX1619_REG_R_REMOTE_LOW 0x08
458d5d45fbSJean Delvare #define MAX1619_REG_W_REMOTE_LOW 0x0E
468d5d45fbSJean Delvare #define MAX1619_REG_R_REMOTE_CRIT 0x10
478d5d45fbSJean Delvare #define MAX1619_REG_W_REMOTE_CRIT 0x12
488d5d45fbSJean Delvare #define MAX1619_REG_R_TCRIT_HYST 0x11
498d5d45fbSJean Delvare #define MAX1619_REG_W_TCRIT_HYST 0x13
508d5d45fbSJean Delvare
518d5d45fbSJean Delvare /*
52a80e8ee6SAndrew Morton * Conversions
538d5d45fbSJean Delvare */
548d5d45fbSJean Delvare
temp_from_reg(int val)55a80e8ee6SAndrew Morton static int temp_from_reg(int val)
56a80e8ee6SAndrew Morton {
57a80e8ee6SAndrew Morton return (val & 0x80 ? val-0x100 : val) * 1000;
58a80e8ee6SAndrew Morton }
59a80e8ee6SAndrew Morton
temp_to_reg(int val)60a80e8ee6SAndrew Morton static int temp_to_reg(int val)
61a80e8ee6SAndrew Morton {
62a80e8ee6SAndrew Morton return (val < 0 ? val+0x100*1000 : val) / 1000;
63a80e8ee6SAndrew Morton }
648d5d45fbSJean Delvare
65f8311196SGuenter Roeck enum temp_index {
66f8311196SGuenter Roeck t_input1 = 0,
67f8311196SGuenter Roeck t_input2,
68f8311196SGuenter Roeck t_low2,
69f8311196SGuenter Roeck t_high2,
70f8311196SGuenter Roeck t_crit2,
71f8311196SGuenter Roeck t_hyst2,
72f8311196SGuenter Roeck t_num_regs
73f8311196SGuenter Roeck };
74f8311196SGuenter Roeck
758d5d45fbSJean Delvare /*
768d5d45fbSJean Delvare * Client data (each client gets its own)
778d5d45fbSJean Delvare */
788d5d45fbSJean Delvare
798d5d45fbSJean Delvare struct max1619_data {
809f1513bdSGuenter Roeck struct i2c_client *client;
819a61bf63SIngo Molnar struct mutex update_lock;
82952a11caSPaul Fertser bool valid; /* false until following fields are valid */
838d5d45fbSJean Delvare unsigned long last_updated; /* in jiffies */
848d5d45fbSJean Delvare
858d5d45fbSJean Delvare /* registers values */
86f8311196SGuenter Roeck u8 temp[t_num_regs]; /* index with enum temp_index */
878d5d45fbSJean Delvare u8 alarms;
888d5d45fbSJean Delvare };
898d5d45fbSJean Delvare
90f8311196SGuenter Roeck static const u8 regs_read[t_num_regs] = {
91f8311196SGuenter Roeck [t_input1] = MAX1619_REG_R_LOCAL_TEMP,
92f8311196SGuenter Roeck [t_input2] = MAX1619_REG_R_REMOTE_TEMP,
93f8311196SGuenter Roeck [t_low2] = MAX1619_REG_R_REMOTE_LOW,
94f8311196SGuenter Roeck [t_high2] = MAX1619_REG_R_REMOTE_HIGH,
95f8311196SGuenter Roeck [t_crit2] = MAX1619_REG_R_REMOTE_CRIT,
96f8311196SGuenter Roeck [t_hyst2] = MAX1619_REG_R_TCRIT_HYST,
97f8311196SGuenter Roeck };
98f8311196SGuenter Roeck
99f8311196SGuenter Roeck static const u8 regs_write[t_num_regs] = {
100f8311196SGuenter Roeck [t_low2] = MAX1619_REG_W_REMOTE_LOW,
101f8311196SGuenter Roeck [t_high2] = MAX1619_REG_W_REMOTE_HIGH,
102f8311196SGuenter Roeck [t_crit2] = MAX1619_REG_W_REMOTE_CRIT,
103f8311196SGuenter Roeck [t_hyst2] = MAX1619_REG_W_TCRIT_HYST,
104f8311196SGuenter Roeck };
105f8311196SGuenter Roeck
max1619_update_device(struct device * dev)10640089a9fSGuenter Roeck static struct max1619_data *max1619_update_device(struct device *dev)
10740089a9fSGuenter Roeck {
1089f1513bdSGuenter Roeck struct max1619_data *data = dev_get_drvdata(dev);
1099f1513bdSGuenter Roeck struct i2c_client *client = data->client;
110f8311196SGuenter Roeck int config, i;
11140089a9fSGuenter Roeck
11240089a9fSGuenter Roeck mutex_lock(&data->update_lock);
11340089a9fSGuenter Roeck
11440089a9fSGuenter Roeck if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
11540089a9fSGuenter Roeck dev_dbg(&client->dev, "Updating max1619 data.\n");
116f8311196SGuenter Roeck for (i = 0; i < t_num_regs; i++)
117f8311196SGuenter Roeck data->temp[i] = i2c_smbus_read_byte_data(client,
118f8311196SGuenter Roeck regs_read[i]);
11940089a9fSGuenter Roeck data->alarms = i2c_smbus_read_byte_data(client,
12040089a9fSGuenter Roeck MAX1619_REG_R_STATUS);
12140089a9fSGuenter Roeck /* If OVERT polarity is low, reverse alarm bit */
12240089a9fSGuenter Roeck config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
12340089a9fSGuenter Roeck if (!(config & 0x20))
12440089a9fSGuenter Roeck data->alarms ^= 0x02;
12540089a9fSGuenter Roeck
12640089a9fSGuenter Roeck data->last_updated = jiffies;
127952a11caSPaul Fertser data->valid = true;
12840089a9fSGuenter Roeck }
12940089a9fSGuenter Roeck
13040089a9fSGuenter Roeck mutex_unlock(&data->update_lock);
13140089a9fSGuenter Roeck
13240089a9fSGuenter Roeck return data;
13340089a9fSGuenter Roeck }
13440089a9fSGuenter Roeck
1358d5d45fbSJean Delvare /*
1368d5d45fbSJean Delvare * Sysfs stuff
1378d5d45fbSJean Delvare */
1388d5d45fbSJean Delvare
temp_show(struct device * dev,struct device_attribute * devattr,char * buf)13921887303SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
140f8311196SGuenter Roeck char *buf)
141f8311196SGuenter Roeck {
142f8311196SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
143f8311196SGuenter Roeck struct max1619_data *data = max1619_update_device(dev);
1448d5d45fbSJean Delvare
145f8311196SGuenter Roeck return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
1468d5d45fbSJean Delvare }
1478d5d45fbSJean Delvare
temp_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)14821887303SGuenter Roeck static ssize_t temp_store(struct device *dev,
14921887303SGuenter Roeck struct device_attribute *devattr, const char *buf,
15021887303SGuenter Roeck size_t count)
151f8311196SGuenter Roeck {
152f8311196SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1539f1513bdSGuenter Roeck struct max1619_data *data = dev_get_drvdata(dev);
1549f1513bdSGuenter Roeck struct i2c_client *client = data->client;
155f8311196SGuenter Roeck long val;
156f8311196SGuenter Roeck int err = kstrtol(buf, 10, &val);
157f8311196SGuenter Roeck if (err)
158f8311196SGuenter Roeck return err;
159f8311196SGuenter Roeck
160f8311196SGuenter Roeck mutex_lock(&data->update_lock);
161f8311196SGuenter Roeck data->temp[attr->index] = temp_to_reg(val);
162f8311196SGuenter Roeck i2c_smbus_write_byte_data(client, regs_write[attr->index],
163f8311196SGuenter Roeck data->temp[attr->index]);
164f8311196SGuenter Roeck mutex_unlock(&data->update_lock);
165f8311196SGuenter Roeck return count;
166f8311196SGuenter Roeck }
1678d5d45fbSJean Delvare
alarms_show(struct device * dev,struct device_attribute * attr,char * buf)16823eb359dSJulia Lawall static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
1698958dfb7SGuenter Roeck char *buf)
1708d5d45fbSJean Delvare {
1718d5d45fbSJean Delvare struct max1619_data *data = max1619_update_device(dev);
1728d5d45fbSJean Delvare return sprintf(buf, "%d\n", data->alarms);
1738d5d45fbSJean Delvare }
1748d5d45fbSJean Delvare
alarm_show(struct device * dev,struct device_attribute * attr,char * buf)17521887303SGuenter Roeck static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
17671062ffcSJean Delvare char *buf)
17771062ffcSJean Delvare {
17871062ffcSJean Delvare int bitnr = to_sensor_dev_attr(attr)->index;
17971062ffcSJean Delvare struct max1619_data *data = max1619_update_device(dev);
18071062ffcSJean Delvare return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
18171062ffcSJean Delvare }
18271062ffcSJean Delvare
18321887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input1);
18421887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, t_input2);
18521887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, t_low2);
18621887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, t_high2);
18721887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, t_crit2);
18821887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_crit_hyst, temp, t_hyst2);
189f8311196SGuenter Roeck
19023eb359dSJulia Lawall static DEVICE_ATTR_RO(alarms);
19121887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 1);
19221887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
19321887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
19421887303SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
1958d5d45fbSJean Delvare
1969f1513bdSGuenter Roeck static struct attribute *max1619_attrs[] = {
197f8311196SGuenter Roeck &sensor_dev_attr_temp1_input.dev_attr.attr,
198f8311196SGuenter Roeck &sensor_dev_attr_temp2_input.dev_attr.attr,
199f8311196SGuenter Roeck &sensor_dev_attr_temp2_min.dev_attr.attr,
200f8311196SGuenter Roeck &sensor_dev_attr_temp2_max.dev_attr.attr,
201f8311196SGuenter Roeck &sensor_dev_attr_temp2_crit.dev_attr.attr,
202f8311196SGuenter Roeck &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
203a5ebe668SJean Delvare
204a5ebe668SJean Delvare &dev_attr_alarms.attr,
20571062ffcSJean Delvare &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
20671062ffcSJean Delvare &sensor_dev_attr_temp2_fault.dev_attr.attr,
20771062ffcSJean Delvare &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
20871062ffcSJean Delvare &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
209a5ebe668SJean Delvare NULL
210a5ebe668SJean Delvare };
2119f1513bdSGuenter Roeck ATTRIBUTE_GROUPS(max1619);
212a5ebe668SJean Delvare
213c6d3f6faSJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
max1619_detect(struct i2c_client * client,struct i2c_board_info * info)214310ec792SJean Delvare static int max1619_detect(struct i2c_client *client,
215c6d3f6faSJean Delvare struct i2c_board_info *info)
2168d5d45fbSJean Delvare {
21752df6440SJean Delvare struct i2c_adapter *adapter = client->adapter;
21852df6440SJean Delvare u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
219b0020e3fSAndrew Morton
2208d5d45fbSJean Delvare if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
221c6d3f6faSJean Delvare return -ENODEV;
2228d5d45fbSJean Delvare
22352df6440SJean Delvare /* detection */
22452df6440SJean Delvare reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
22552df6440SJean Delvare reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
22652df6440SJean Delvare reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
2278d5d45fbSJean Delvare if ((reg_config & 0x03) != 0x00
2288d5d45fbSJean Delvare || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
22952df6440SJean Delvare dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
23052df6440SJean Delvare client->addr);
231c6d3f6faSJean Delvare return -ENODEV;
2328d5d45fbSJean Delvare }
2338d5d45fbSJean Delvare
23452df6440SJean Delvare /* identification */
23552df6440SJean Delvare man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
23652df6440SJean Delvare chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
23752df6440SJean Delvare if (man_id != 0x4D || chip_id != 0x04) {
2388d5d45fbSJean Delvare dev_info(&adapter->dev,
23952df6440SJean Delvare "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
24052df6440SJean Delvare man_id, chip_id);
241c6d3f6faSJean Delvare return -ENODEV;
2428d5d45fbSJean Delvare }
2438d5d45fbSJean Delvare
244f2f394dbSWolfram Sang strscpy(info->type, "max1619", I2C_NAME_SIZE);
245b0020e3fSAndrew Morton
246c6d3f6faSJean Delvare return 0;
247c6d3f6faSJean Delvare }
248c6d3f6faSJean Delvare
max1619_init_client(struct i2c_client * client)24940089a9fSGuenter Roeck static void max1619_init_client(struct i2c_client *client)
25040089a9fSGuenter Roeck {
25140089a9fSGuenter Roeck u8 config;
25240089a9fSGuenter Roeck
25340089a9fSGuenter Roeck /*
25440089a9fSGuenter Roeck * Start the conversions.
25540089a9fSGuenter Roeck */
25640089a9fSGuenter Roeck i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
25740089a9fSGuenter Roeck 5); /* 2 Hz */
25840089a9fSGuenter Roeck config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
25940089a9fSGuenter Roeck if (config & 0x40)
26040089a9fSGuenter Roeck i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
26140089a9fSGuenter Roeck config & 0xBF); /* run */
26240089a9fSGuenter Roeck }
26340089a9fSGuenter Roeck
max1619_probe(struct i2c_client * new_client)26467487038SStephen Kitt static int max1619_probe(struct i2c_client *new_client)
265c6d3f6faSJean Delvare {
266c6d3f6faSJean Delvare struct max1619_data *data;
2679f1513bdSGuenter Roeck struct device *hwmon_dev;
268c6d3f6faSJean Delvare
269215690a4SGuenter Roeck data = devm_kzalloc(&new_client->dev, sizeof(struct max1619_data),
270215690a4SGuenter Roeck GFP_KERNEL);
271215690a4SGuenter Roeck if (!data)
272215690a4SGuenter Roeck return -ENOMEM;
273c6d3f6faSJean Delvare
2749f1513bdSGuenter Roeck data->client = new_client;
2759a61bf63SIngo Molnar mutex_init(&data->update_lock);
2768d5d45fbSJean Delvare
2778d5d45fbSJean Delvare /* Initialize the MAX1619 chip */
2788d5d45fbSJean Delvare max1619_init_client(new_client);
2798d5d45fbSJean Delvare
2809f1513bdSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
2819f1513bdSGuenter Roeck new_client->name,
2829f1513bdSGuenter Roeck data,
2839f1513bdSGuenter Roeck max1619_groups);
2849f1513bdSGuenter Roeck return PTR_ERR_OR_ZERO(hwmon_dev);
2858d5d45fbSJean Delvare }
2868d5d45fbSJean Delvare
28740089a9fSGuenter Roeck static const struct i2c_device_id max1619_id[] = {
28840089a9fSGuenter Roeck { "max1619", 0 },
28940089a9fSGuenter Roeck { }
29040089a9fSGuenter Roeck };
29140089a9fSGuenter Roeck MODULE_DEVICE_TABLE(i2c, max1619_id);
2928d5d45fbSJean Delvare
293fd53f621SAlan Tull #ifdef CONFIG_OF
294fd53f621SAlan Tull static const struct of_device_id max1619_of_match[] = {
295fd53f621SAlan Tull { .compatible = "maxim,max1619", },
296fd53f621SAlan Tull {},
297fd53f621SAlan Tull };
298fd53f621SAlan Tull
299fd53f621SAlan Tull MODULE_DEVICE_TABLE(of, max1619_of_match);
300fd53f621SAlan Tull #endif
301fd53f621SAlan Tull
30240089a9fSGuenter Roeck static struct i2c_driver max1619_driver = {
30340089a9fSGuenter Roeck .class = I2C_CLASS_HWMON,
30440089a9fSGuenter Roeck .driver = {
30540089a9fSGuenter Roeck .name = "max1619",
306fd53f621SAlan Tull .of_match_table = of_match_ptr(max1619_of_match),
30740089a9fSGuenter Roeck },
308*1975d167SUwe Kleine-König .probe = max1619_probe,
30940089a9fSGuenter Roeck .id_table = max1619_id,
31040089a9fSGuenter Roeck .detect = max1619_detect,
31140089a9fSGuenter Roeck .address_list = normal_i2c,
31240089a9fSGuenter Roeck };
3138d5d45fbSJean Delvare
314f0967eeaSAxel Lin module_i2c_driver(max1619_driver);
3158d5d45fbSJean Delvare
3167c81c60fSJean Delvare MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
3178d5d45fbSJean Delvare MODULE_DESCRIPTION("MAX1619 sensor driver");
3188d5d45fbSJean Delvare MODULE_LICENSE("GPL");
319