blob: beb0d61bcd821c9baef68eec568e7a3e6bf74004 [file] [log] [blame]
Thomas Gleixner06776072019-05-29 16:57:28 -07001// SPDX-License-Identifier: GPL-2.0-only
Adrien Demarez4e233cb2009-12-09 20:35:50 +01002/*
3 * LM73 Sensor driver
4 * Based on LM75
5 *
6 * Copyright (C) 2007, CenoSYS (www.cenosys.com).
7 * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
8 *
9 * Guillaume Ligneul <guillaume.ligneul@gmail.com>
10 * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
11 * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
Chris Verges2bf92332013-01-05 01:41:20 -080012 * Chris Verges <kg4ysn@gmail.com>
Adrien Demarez4e233cb2009-12-09 20:35:50 +010013 */
14
15#include <linux/module.h>
16#include <linux/init.h>
Adrien Demarez4e233cb2009-12-09 20:35:50 +010017#include <linux/i2c.h>
18#include <linux/hwmon.h>
19#include <linux/hwmon-sysfs.h>
20#include <linux/err.h>
21
22
23/* Addresses scanned */
24static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
25 0x4d, 0x4e, I2C_CLIENT_END };
26
Adrien Demarez4e233cb2009-12-09 20:35:50 +010027/* LM73 registers */
28#define LM73_REG_INPUT 0x00
29#define LM73_REG_CONF 0x01
30#define LM73_REG_MAX 0x02
31#define LM73_REG_MIN 0x03
32#define LM73_REG_CTRL 0x04
33#define LM73_REG_ID 0x07
34
Jean Delvare90f41022011-11-04 12:00:47 +010035#define LM73_ID 0x9001 /* 0x0190, byte-swapped */
Adrien Demarez4e233cb2009-12-09 20:35:50 +010036#define DRVNAME "lm73"
Guenter Roeck91bba682013-01-10 21:16:47 -080037#define LM73_TEMP_MIN (-256000 / 250)
38#define LM73_TEMP_MAX (255750 / 250)
Adrien Demarez4e233cb2009-12-09 20:35:50 +010039
Chris Verges8c14d122013-01-05 01:41:19 -080040#define LM73_CTRL_RES_SHIFT 5
41#define LM73_CTRL_RES_MASK (BIT(5) | BIT(6))
42#define LM73_CTRL_TO_MASK BIT(7)
Adrien Demarez4e233cb2009-12-09 20:35:50 +010043
Chris Verges2bf92332013-01-05 01:41:20 -080044#define LM73_CTRL_HI_SHIFT 2
45#define LM73_CTRL_LO_SHIFT 1
46
Chris Verges8c14d122013-01-05 01:41:19 -080047static const unsigned short lm73_convrates[] = {
48 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
49 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
50 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
51 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
52};
53
54struct lm73_data {
Guenter Roeck37102632013-09-01 18:32:52 -070055 struct i2c_client *client;
Chris Verges8c14d122013-01-05 01:41:19 -080056 struct mutex lock;
57 u8 ctrl; /* control register value */
58};
59
60/*-----------------------------------------------------------------------*/
Adrien Demarez4e233cb2009-12-09 20:35:50 +010061
Guenter Roeck0f875ac2018-12-10 14:02:11 -080062static ssize_t temp_store(struct device *dev, struct device_attribute *da,
63 const char *buf, size_t count)
Adrien Demarez4e233cb2009-12-09 20:35:50 +010064{
65 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck37102632013-09-01 18:32:52 -070066 struct lm73_data *data = dev_get_drvdata(dev);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010067 long temp;
68 short value;
Chris Verges06029342012-12-21 01:58:34 -080069 s32 err;
Adrien Demarez4e233cb2009-12-09 20:35:50 +010070
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +010071 int status = kstrtol(buf, 10, &temp);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010072 if (status < 0)
73 return status;
74
75 /* Write value */
Guenter Roeck91bba682013-01-10 21:16:47 -080076 value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
Guenter Roeck37102632013-09-01 18:32:52 -070077 err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
Chris Verges06029342012-12-21 01:58:34 -080078 return (err < 0) ? err : count;
Adrien Demarez4e233cb2009-12-09 20:35:50 +010079}
80
Guenter Roeck0f875ac2018-12-10 14:02:11 -080081static ssize_t temp_show(struct device *dev, struct device_attribute *da,
Adrien Demarez4e233cb2009-12-09 20:35:50 +010082 char *buf)
83{
84 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck37102632013-09-01 18:32:52 -070085 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges06029342012-12-21 01:58:34 -080086 int temp;
87
Guenter Roeck37102632013-09-01 18:32:52 -070088 s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
Chris Verges06029342012-12-21 01:58:34 -080089 if (err < 0)
90 return err;
91
Adrien Demarez4e233cb2009-12-09 20:35:50 +010092 /* use integer division instead of equivalent right shift to
93 guarantee arithmetic shift and preserve the sign */
Chris Verges06029342012-12-21 01:58:34 -080094 temp = (((s16) err) * 250) / 32;
95 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010096}
97
Guenter Roeck0f875ac2018-12-10 14:02:11 -080098static ssize_t convrate_store(struct device *dev, struct device_attribute *da,
99 const char *buf, size_t count)
Chris Verges8c14d122013-01-05 01:41:19 -0800100{
Guenter Roeck37102632013-09-01 18:32:52 -0700101 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges8c14d122013-01-05 01:41:19 -0800102 unsigned long convrate;
103 s32 err;
104 int res = 0;
105
106 err = kstrtoul(buf, 10, &convrate);
107 if (err < 0)
108 return err;
109
110 /*
111 * Convert the desired conversion rate into register bits.
112 * res is already initialized, and everything past the second-to-last
113 * value in the array is treated as belonging to the last value
114 * in the array.
115 */
116 while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
117 convrate > lm73_convrates[res])
118 res++;
119
120 mutex_lock(&data->lock);
121 data->ctrl &= LM73_CTRL_TO_MASK;
122 data->ctrl |= res << LM73_CTRL_RES_SHIFT;
Guenter Roeck37102632013-09-01 18:32:52 -0700123 err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
124 data->ctrl);
Chris Verges8c14d122013-01-05 01:41:19 -0800125 mutex_unlock(&data->lock);
126
127 if (err < 0)
128 return err;
129
130 return count;
131}
132
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800133static ssize_t convrate_show(struct device *dev, struct device_attribute *da,
Chris Verges8c14d122013-01-05 01:41:19 -0800134 char *buf)
135{
Guenter Roeck37102632013-09-01 18:32:52 -0700136 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges8c14d122013-01-05 01:41:19 -0800137 int res;
138
139 res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
140 return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
141}
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100142
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800143static ssize_t maxmin_alarm_show(struct device *dev,
Chris Verges2bf92332013-01-05 01:41:20 -0800144 struct device_attribute *da, char *buf)
145{
Chris Verges2bf92332013-01-05 01:41:20 -0800146 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck37102632013-09-01 18:32:52 -0700147 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges2bf92332013-01-05 01:41:20 -0800148 s32 ctrl;
149
150 mutex_lock(&data->lock);
Guenter Roeck37102632013-09-01 18:32:52 -0700151 ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
Chris Verges2bf92332013-01-05 01:41:20 -0800152 if (ctrl < 0)
153 goto abort;
154 data->ctrl = ctrl;
155 mutex_unlock(&data->lock);
156
157 return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
158
159abort:
160 mutex_unlock(&data->lock);
161 return ctrl;
162}
163
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100164/*-----------------------------------------------------------------------*/
165
166/* sysfs attributes for hwmon */
167
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800168static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX);
169static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN);
170static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT);
171static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0);
172static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm,
173 LM73_CTRL_HI_SHIFT);
174static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm,
175 LM73_CTRL_LO_SHIFT);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100176
Guenter Roeck37102632013-09-01 18:32:52 -0700177static struct attribute *lm73_attrs[] = {
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100178 &sensor_dev_attr_temp1_input.dev_attr.attr,
179 &sensor_dev_attr_temp1_max.dev_attr.attr,
180 &sensor_dev_attr_temp1_min.dev_attr.attr,
Chris Verges8c14d122013-01-05 01:41:19 -0800181 &sensor_dev_attr_update_interval.dev_attr.attr,
Chris Verges2bf92332013-01-05 01:41:20 -0800182 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
183 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100184 NULL
185};
Guenter Roeck37102632013-09-01 18:32:52 -0700186ATTRIBUTE_GROUPS(lm73);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100187
188/*-----------------------------------------------------------------------*/
189
190/* device probe and removal */
191
192static int
Stephen Kitt91ed7c42020-08-13 18:23:00 +0200193lm73_probe(struct i2c_client *client)
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100194{
Guenter Roeck37102632013-09-01 18:32:52 -0700195 struct device *dev = &client->dev;
196 struct device *hwmon_dev;
Chris Verges8c14d122013-01-05 01:41:19 -0800197 struct lm73_data *data;
198 int ctrl;
199
Guenter Roeck37102632013-09-01 18:32:52 -0700200 data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
Chris Verges8c14d122013-01-05 01:41:19 -0800201 if (!data)
202 return -ENOMEM;
203
Guenter Roeck37102632013-09-01 18:32:52 -0700204 data->client = client;
Chris Verges8c14d122013-01-05 01:41:19 -0800205 mutex_init(&data->lock);
206
207 ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
208 if (ctrl < 0)
209 return ctrl;
210 data->ctrl = ctrl;
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100211
Guenter Roeck37102632013-09-01 18:32:52 -0700212 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
213 data, lm73_groups);
214 if (IS_ERR(hwmon_dev))
215 return PTR_ERR(hwmon_dev);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100216
Guenter Roeck37102632013-09-01 18:32:52 -0700217 dev_info(dev, "sensor '%s'\n", client->name);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100218
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100219 return 0;
220}
221
222static const struct i2c_device_id lm73_ids[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100223 { "lm73", 0 },
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100224 { /* LIST END */ }
225};
226MODULE_DEVICE_TABLE(i2c, lm73_ids);
227
228/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100229static int lm73_detect(struct i2c_client *new_client,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100230 struct i2c_board_info *info)
231{
232 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100233 int id, ctrl, conf;
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100234
235 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
236 I2C_FUNC_SMBUS_WORD_DATA))
237 return -ENODEV;
238
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100239 /*
240 * Do as much detection as possible with byte reads first, as word
241 * reads can confuse other devices.
242 */
243 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
244 if (ctrl < 0 || (ctrl & 0x10))
245 return -ENODEV;
246
247 conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
248 if (conf < 0 || (conf & 0x0c))
249 return -ENODEV;
250
251 id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
252 if (id < 0 || id != (LM73_ID & 0xff))
253 return -ENODEV;
254
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100255 /* Check device ID */
256 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100257 if (id < 0 || id != LM73_ID)
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100258 return -ENODEV;
259
260 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
261
262 return 0;
263}
264
Henry Shen0454e792020-02-12 16:06:15 +1300265static const struct of_device_id lm73_of_match[] = {
266 {
267 .compatible = "ti,lm73",
268 },
269 { },
270};
271
272MODULE_DEVICE_TABLE(of, lm73_of_match);
273
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100274static struct i2c_driver lm73_driver = {
275 .class = I2C_CLASS_HWMON,
276 .driver = {
277 .name = "lm73",
Henry Shen0454e792020-02-12 16:06:15 +1300278 .of_match_table = lm73_of_match,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100279 },
Stephen Kitt91ed7c42020-08-13 18:23:00 +0200280 .probe_new = lm73_probe,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100281 .id_table = lm73_ids,
282 .detect = lm73_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100283 .address_list = normal_i2c,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100284};
285
Axel Linf0967ee2012-01-20 15:38:18 +0800286module_i2c_driver(lm73_driver);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100287
288MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
289MODULE_DESCRIPTION("LM73 driver");
290MODULE_LICENSE("GPL");