174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
260994698SRoland Stigge /*
360994698SRoland Stigge * ds620.c - Support for temperature sensor and thermostat DS620
460994698SRoland Stigge *
560994698SRoland Stigge * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
660994698SRoland Stigge *
760994698SRoland Stigge * based on ds1621.c by Christian W. Zuckschwerdt <zany@triq.net>
860994698SRoland Stigge */
960994698SRoland Stigge
1060994698SRoland Stigge #include <linux/module.h>
1160994698SRoland Stigge #include <linux/init.h>
1260994698SRoland Stigge #include <linux/slab.h>
1360994698SRoland Stigge #include <linux/jiffies.h>
1460994698SRoland Stigge #include <linux/i2c.h>
1560994698SRoland Stigge #include <linux/hwmon.h>
1660994698SRoland Stigge #include <linux/hwmon-sysfs.h>
1760994698SRoland Stigge #include <linux/err.h>
1860994698SRoland Stigge #include <linux/mutex.h>
1960994698SRoland Stigge #include <linux/sysfs.h>
20570999f3SWolfram Sang #include <linux/platform_data/ds620.h>
2160994698SRoland Stigge
2260994698SRoland Stigge /*
2360994698SRoland Stigge * Many DS620 constants specified below
2460994698SRoland Stigge * 15 14 13 12 11 10 09 08
2560994698SRoland Stigge * |Done|NVB |THF |TLF |R1 |R0 |AUTOC|1SHOT|
2660994698SRoland Stigge *
2760994698SRoland Stigge * 07 06 05 04 03 02 01 00
2860994698SRoland Stigge * |PO2 |PO1 |A2 |A1 |A0 | | | |
2960994698SRoland Stigge */
3060994698SRoland Stigge #define DS620_REG_CONFIG_DONE 0x8000
3160994698SRoland Stigge #define DS620_REG_CONFIG_NVB 0x4000
3260994698SRoland Stigge #define DS620_REG_CONFIG_THF 0x2000
3360994698SRoland Stigge #define DS620_REG_CONFIG_TLF 0x1000
3460994698SRoland Stigge #define DS620_REG_CONFIG_R1 0x0800
3560994698SRoland Stigge #define DS620_REG_CONFIG_R0 0x0400
3660994698SRoland Stigge #define DS620_REG_CONFIG_AUTOC 0x0200
3760994698SRoland Stigge #define DS620_REG_CONFIG_1SHOT 0x0100
3860994698SRoland Stigge #define DS620_REG_CONFIG_PO2 0x0080
3960994698SRoland Stigge #define DS620_REG_CONFIG_PO1 0x0040
4060994698SRoland Stigge #define DS620_REG_CONFIG_A2 0x0020
4160994698SRoland Stigge #define DS620_REG_CONFIG_A1 0x0010
4260994698SRoland Stigge #define DS620_REG_CONFIG_A0 0x0008
4360994698SRoland Stigge
4460994698SRoland Stigge /* The DS620 registers */
4560994698SRoland Stigge static const u8 DS620_REG_TEMP[3] = {
4660994698SRoland Stigge 0xAA, /* input, word, RO */
4760994698SRoland Stigge 0xA2, /* min, word, RW */
4860994698SRoland Stigge 0xA0, /* max, word, RW */
4960994698SRoland Stigge };
5060994698SRoland Stigge
5160994698SRoland Stigge #define DS620_REG_CONF 0xAC /* word, RW */
5260994698SRoland Stigge #define DS620_COM_START 0x51 /* no data */
5360994698SRoland Stigge #define DS620_COM_STOP 0x22 /* no data */
5460994698SRoland Stigge
5560994698SRoland Stigge /* Each client has this additional data */
5660994698SRoland Stigge struct ds620_data {
57f073b994SAxel Lin struct i2c_client *client;
5860994698SRoland Stigge struct mutex update_lock;
59952a11caSPaul Fertser bool valid; /* true if following fields are valid */
6060994698SRoland Stigge unsigned long last_updated; /* In jiffies */
6160994698SRoland Stigge
62cc41d586SRoland Stigge s16 temp[3]; /* Register values, word */
6360994698SRoland Stigge };
6460994698SRoland Stigge
ds620_init_client(struct i2c_client * client)6560994698SRoland Stigge static void ds620_init_client(struct i2c_client *client)
6660994698SRoland Stigge {
67a8b3a3a5SJingoo Han struct ds620_platform_data *ds620_info = dev_get_platdata(&client->dev);
6860994698SRoland Stigge u16 conf, new_conf;
6960994698SRoland Stigge
7060994698SRoland Stigge new_conf = conf =
7190f4102cSJean Delvare i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
7260994698SRoland Stigge
7360994698SRoland Stigge /* switch to continuous conversion mode */
7460994698SRoland Stigge new_conf &= ~DS620_REG_CONFIG_1SHOT;
7560994698SRoland Stigge /* already high at power-on, but don't trust the BIOS! */
7660994698SRoland Stigge new_conf |= DS620_REG_CONFIG_PO2;
7760994698SRoland Stigge /* thermostat mode according to platform data */
7860994698SRoland Stigge if (ds620_info && ds620_info->pomode == 1)
7960994698SRoland Stigge new_conf &= ~DS620_REG_CONFIG_PO1; /* PO_LOW */
8060994698SRoland Stigge else if (ds620_info && ds620_info->pomode == 2)
8160994698SRoland Stigge new_conf |= DS620_REG_CONFIG_PO1; /* PO_HIGH */
8260994698SRoland Stigge else
8360994698SRoland Stigge new_conf &= ~DS620_REG_CONFIG_PO2; /* always low */
8460994698SRoland Stigge /* with highest precision */
8560994698SRoland Stigge new_conf |= DS620_REG_CONFIG_R1 | DS620_REG_CONFIG_R0;
8660994698SRoland Stigge
8760994698SRoland Stigge if (conf != new_conf)
8890f4102cSJean Delvare i2c_smbus_write_word_swapped(client, DS620_REG_CONF, new_conf);
8960994698SRoland Stigge
9060994698SRoland Stigge /* start conversion */
9160994698SRoland Stigge i2c_smbus_write_byte(client, DS620_COM_START);
9260994698SRoland Stigge }
9360994698SRoland Stigge
ds620_update_client(struct device * dev)9460994698SRoland Stigge static struct ds620_data *ds620_update_client(struct device *dev)
9560994698SRoland Stigge {
96f073b994SAxel Lin struct ds620_data *data = dev_get_drvdata(dev);
97f073b994SAxel Lin struct i2c_client *client = data->client;
9860994698SRoland Stigge struct ds620_data *ret = data;
9960994698SRoland Stigge
10060994698SRoland Stigge mutex_lock(&data->update_lock);
10160994698SRoland Stigge
10260994698SRoland Stigge if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
10360994698SRoland Stigge || !data->valid) {
10460994698SRoland Stigge int i;
10560994698SRoland Stigge int res;
10660994698SRoland Stigge
10760994698SRoland Stigge dev_dbg(&client->dev, "Starting ds620 update\n");
10860994698SRoland Stigge
10960994698SRoland Stigge for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
11090f4102cSJean Delvare res = i2c_smbus_read_word_swapped(client,
11160994698SRoland Stigge DS620_REG_TEMP[i]);
11260994698SRoland Stigge if (res < 0) {
11360994698SRoland Stigge ret = ERR_PTR(res);
11460994698SRoland Stigge goto abort;
11560994698SRoland Stigge }
11660994698SRoland Stigge
11760994698SRoland Stigge data->temp[i] = res;
11860994698SRoland Stigge }
11960994698SRoland Stigge
12060994698SRoland Stigge data->last_updated = jiffies;
121952a11caSPaul Fertser data->valid = true;
12260994698SRoland Stigge }
12360994698SRoland Stigge abort:
12460994698SRoland Stigge mutex_unlock(&data->update_lock);
12560994698SRoland Stigge
12660994698SRoland Stigge return ret;
12760994698SRoland Stigge }
12860994698SRoland Stigge
temp_show(struct device * dev,struct device_attribute * da,char * buf)12957549f33SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *da,
13060994698SRoland Stigge char *buf)
13160994698SRoland Stigge {
13260994698SRoland Stigge struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
13360994698SRoland Stigge struct ds620_data *data = ds620_update_client(dev);
13460994698SRoland Stigge
13560994698SRoland Stigge if (IS_ERR(data))
13660994698SRoland Stigge return PTR_ERR(data);
13760994698SRoland Stigge
13860994698SRoland Stigge return sprintf(buf, "%d\n", ((data->temp[attr->index] / 8) * 625) / 10);
13960994698SRoland Stigge }
14060994698SRoland Stigge
temp_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)14157549f33SGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *da,
14260994698SRoland Stigge const char *buf, size_t count)
14360994698SRoland Stigge {
14460994698SRoland Stigge int res;
14560994698SRoland Stigge long val;
14660994698SRoland Stigge
14760994698SRoland Stigge struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
148f073b994SAxel Lin struct ds620_data *data = dev_get_drvdata(dev);
149f073b994SAxel Lin struct i2c_client *client = data->client;
15060994698SRoland Stigge
151179c4fdbSFrans Meulenbroeks res = kstrtol(buf, 10, &val);
15260994698SRoland Stigge
15360994698SRoland Stigge if (res)
15460994698SRoland Stigge return res;
15560994698SRoland Stigge
156e36ce99eSGuenter Roeck val = (clamp_val(val, -128000, 128000) * 10 / 625) * 8;
15760994698SRoland Stigge
15860994698SRoland Stigge mutex_lock(&data->update_lock);
15960994698SRoland Stigge data->temp[attr->index] = val;
16090f4102cSJean Delvare i2c_smbus_write_word_swapped(client, DS620_REG_TEMP[attr->index],
16160994698SRoland Stigge data->temp[attr->index]);
16260994698SRoland Stigge mutex_unlock(&data->update_lock);
16360994698SRoland Stigge return count;
16460994698SRoland Stigge }
16560994698SRoland Stigge
alarm_show(struct device * dev,struct device_attribute * da,char * buf)16657549f33SGuenter Roeck static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
16760994698SRoland Stigge char *buf)
16860994698SRoland Stigge {
16960994698SRoland Stigge struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
17060994698SRoland Stigge struct ds620_data *data = ds620_update_client(dev);
171f073b994SAxel Lin struct i2c_client *client;
17260994698SRoland Stigge u16 conf, new_conf;
17360994698SRoland Stigge int res;
17460994698SRoland Stigge
17560994698SRoland Stigge if (IS_ERR(data))
17660994698SRoland Stigge return PTR_ERR(data);
17760994698SRoland Stigge
178f073b994SAxel Lin client = data->client;
179f073b994SAxel Lin
18060994698SRoland Stigge /* reset alarms if necessary */
18190f4102cSJean Delvare res = i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
18260994698SRoland Stigge if (res < 0)
18360994698SRoland Stigge return res;
18460994698SRoland Stigge
18590f4102cSJean Delvare new_conf = conf = res;
18660994698SRoland Stigge new_conf &= ~attr->index;
18760994698SRoland Stigge if (conf != new_conf) {
18890f4102cSJean Delvare res = i2c_smbus_write_word_swapped(client, DS620_REG_CONF,
18990f4102cSJean Delvare new_conf);
19060994698SRoland Stigge if (res < 0)
19160994698SRoland Stigge return res;
19260994698SRoland Stigge }
19360994698SRoland Stigge
19460994698SRoland Stigge return sprintf(buf, "%d\n", !!(conf & attr->index));
19560994698SRoland Stigge }
19660994698SRoland Stigge
19757549f33SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
19857549f33SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 1);
19957549f33SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
20057549f33SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, DS620_REG_CONFIG_TLF);
20157549f33SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, DS620_REG_CONFIG_THF);
20260994698SRoland Stigge
203f073b994SAxel Lin static struct attribute *ds620_attrs[] = {
20460994698SRoland Stigge &sensor_dev_attr_temp1_input.dev_attr.attr,
20560994698SRoland Stigge &sensor_dev_attr_temp1_min.dev_attr.attr,
20660994698SRoland Stigge &sensor_dev_attr_temp1_max.dev_attr.attr,
20760994698SRoland Stigge &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
20860994698SRoland Stigge &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
20960994698SRoland Stigge NULL
21060994698SRoland Stigge };
21160994698SRoland Stigge
212f073b994SAxel Lin ATTRIBUTE_GROUPS(ds620);
21360994698SRoland Stigge
ds620_probe(struct i2c_client * client)21467487038SStephen Kitt static int ds620_probe(struct i2c_client *client)
21560994698SRoland Stigge {
216f073b994SAxel Lin struct device *dev = &client->dev;
217f073b994SAxel Lin struct device *hwmon_dev;
21860994698SRoland Stigge struct ds620_data *data;
21960994698SRoland Stigge
220f073b994SAxel Lin data = devm_kzalloc(dev, sizeof(struct ds620_data), GFP_KERNEL);
2213aa9d1dfSGuenter Roeck if (!data)
2223aa9d1dfSGuenter Roeck return -ENOMEM;
22360994698SRoland Stigge
224f073b994SAxel Lin data->client = client;
22560994698SRoland Stigge mutex_init(&data->update_lock);
22660994698SRoland Stigge
22760994698SRoland Stigge /* Initialize the DS620 chip */
22860994698SRoland Stigge ds620_init_client(client);
22960994698SRoland Stigge
230f073b994SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
231f073b994SAxel Lin data, ds620_groups);
232f073b994SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev);
23360994698SRoland Stigge }
23460994698SRoland Stigge
23560994698SRoland Stigge static const struct i2c_device_id ds620_id[] = {
236*d8a66f36SUwe Kleine-König {"ds620"},
23760994698SRoland Stigge {}
23860994698SRoland Stigge };
23960994698SRoland Stigge
24060994698SRoland Stigge MODULE_DEVICE_TABLE(i2c, ds620_id);
24160994698SRoland Stigge
24260994698SRoland Stigge /* This is the driver that will be inserted */
24360994698SRoland Stigge static struct i2c_driver ds620_driver = {
24460994698SRoland Stigge .driver = {
24560994698SRoland Stigge .name = "ds620",
24660994698SRoland Stigge },
2471975d167SUwe Kleine-König .probe = ds620_probe,
24860994698SRoland Stigge .id_table = ds620_id,
24960994698SRoland Stigge };
25060994698SRoland Stigge
251f0967eeaSAxel Lin module_i2c_driver(ds620_driver);
25260994698SRoland Stigge
25360994698SRoland Stigge MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
25460994698SRoland Stigge MODULE_DESCRIPTION("DS620 driver");
25560994698SRoland Stigge MODULE_LICENSE("GPL");
256