blob: d1d728aa31d24ff5055a210f11fdf94ff03db0d8 [file] [log] [blame]
Adrien Demarez4e233cb2009-12-09 20:35:50 +01001/*
2 * LM73 Sensor driver
3 * Based on LM75
4 *
5 * Copyright (C) 2007, CenoSYS (www.cenosys.com).
6 * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
7 *
8 * Guillaume Ligneul <guillaume.ligneul@gmail.com>
9 * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
10 * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
Chris Verges2bf92332013-01-05 01:41:20 -080011 * Chris Verges <kg4ysn@gmail.com>
Adrien Demarez4e233cb2009-12-09 20:35:50 +010012 *
13 * This software program is licensed subject to the GNU General Public License
14 * (GPL).Version 2,June 1991, available at
15 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Adrien Demarez4e233cb2009-12-09 20:35:50 +010020#include <linux/i2c.h>
21#include <linux/hwmon.h>
22#include <linux/hwmon-sysfs.h>
23#include <linux/err.h>
24
25
26/* Addresses scanned */
27static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
28 0x4d, 0x4e, I2C_CLIENT_END };
29
Adrien Demarez4e233cb2009-12-09 20:35:50 +010030/* LM73 registers */
31#define LM73_REG_INPUT 0x00
32#define LM73_REG_CONF 0x01
33#define LM73_REG_MAX 0x02
34#define LM73_REG_MIN 0x03
35#define LM73_REG_CTRL 0x04
36#define LM73_REG_ID 0x07
37
Jean Delvare90f41022011-11-04 12:00:47 +010038#define LM73_ID 0x9001 /* 0x0190, byte-swapped */
Adrien Demarez4e233cb2009-12-09 20:35:50 +010039#define DRVNAME "lm73"
Guenter Roeck91bba682013-01-10 21:16:47 -080040#define LM73_TEMP_MIN (-256000 / 250)
41#define LM73_TEMP_MAX (255750 / 250)
Adrien Demarez4e233cb2009-12-09 20:35:50 +010042
Chris Verges8c14d122013-01-05 01:41:19 -080043#define LM73_CTRL_RES_SHIFT 5
44#define LM73_CTRL_RES_MASK (BIT(5) | BIT(6))
45#define LM73_CTRL_TO_MASK BIT(7)
Adrien Demarez4e233cb2009-12-09 20:35:50 +010046
Chris Verges2bf92332013-01-05 01:41:20 -080047#define LM73_CTRL_HI_SHIFT 2
48#define LM73_CTRL_LO_SHIFT 1
49
Chris Verges8c14d122013-01-05 01:41:19 -080050static const unsigned short lm73_convrates[] = {
51 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
52 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
53 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
54 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
55};
56
57struct lm73_data {
Guenter Roeck37102632013-09-01 18:32:52 -070058 struct i2c_client *client;
Chris Verges8c14d122013-01-05 01:41:19 -080059 struct mutex lock;
60 u8 ctrl; /* control register value */
61};
62
63/*-----------------------------------------------------------------------*/
Adrien Demarez4e233cb2009-12-09 20:35:50 +010064
Guenter Roeck0f875ac2018-12-10 14:02:11 -080065static ssize_t temp_store(struct device *dev, struct device_attribute *da,
66 const char *buf, size_t count)
Adrien Demarez4e233cb2009-12-09 20:35:50 +010067{
68 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck37102632013-09-01 18:32:52 -070069 struct lm73_data *data = dev_get_drvdata(dev);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010070 long temp;
71 short value;
Chris Verges06029342012-12-21 01:58:34 -080072 s32 err;
Adrien Demarez4e233cb2009-12-09 20:35:50 +010073
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +010074 int status = kstrtol(buf, 10, &temp);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010075 if (status < 0)
76 return status;
77
78 /* Write value */
Guenter Roeck91bba682013-01-10 21:16:47 -080079 value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
Guenter Roeck37102632013-09-01 18:32:52 -070080 err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
Chris Verges06029342012-12-21 01:58:34 -080081 return (err < 0) ? err : count;
Adrien Demarez4e233cb2009-12-09 20:35:50 +010082}
83
Guenter Roeck0f875ac2018-12-10 14:02:11 -080084static ssize_t temp_show(struct device *dev, struct device_attribute *da,
Adrien Demarez4e233cb2009-12-09 20:35:50 +010085 char *buf)
86{
87 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck37102632013-09-01 18:32:52 -070088 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges06029342012-12-21 01:58:34 -080089 int temp;
90
Guenter Roeck37102632013-09-01 18:32:52 -070091 s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
Chris Verges06029342012-12-21 01:58:34 -080092 if (err < 0)
93 return err;
94
Adrien Demarez4e233cb2009-12-09 20:35:50 +010095 /* use integer division instead of equivalent right shift to
96 guarantee arithmetic shift and preserve the sign */
Chris Verges06029342012-12-21 01:58:34 -080097 temp = (((s16) err) * 250) / 32;
98 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010099}
100
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800101static ssize_t convrate_store(struct device *dev, struct device_attribute *da,
102 const char *buf, size_t count)
Chris Verges8c14d122013-01-05 01:41:19 -0800103{
Guenter Roeck37102632013-09-01 18:32:52 -0700104 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges8c14d122013-01-05 01:41:19 -0800105 unsigned long convrate;
106 s32 err;
107 int res = 0;
108
109 err = kstrtoul(buf, 10, &convrate);
110 if (err < 0)
111 return err;
112
113 /*
114 * Convert the desired conversion rate into register bits.
115 * res is already initialized, and everything past the second-to-last
116 * value in the array is treated as belonging to the last value
117 * in the array.
118 */
119 while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
120 convrate > lm73_convrates[res])
121 res++;
122
123 mutex_lock(&data->lock);
124 data->ctrl &= LM73_CTRL_TO_MASK;
125 data->ctrl |= res << LM73_CTRL_RES_SHIFT;
Guenter Roeck37102632013-09-01 18:32:52 -0700126 err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
127 data->ctrl);
Chris Verges8c14d122013-01-05 01:41:19 -0800128 mutex_unlock(&data->lock);
129
130 if (err < 0)
131 return err;
132
133 return count;
134}
135
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800136static ssize_t convrate_show(struct device *dev, struct device_attribute *da,
Chris Verges8c14d122013-01-05 01:41:19 -0800137 char *buf)
138{
Guenter Roeck37102632013-09-01 18:32:52 -0700139 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges8c14d122013-01-05 01:41:19 -0800140 int res;
141
142 res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
143 return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
144}
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100145
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800146static ssize_t maxmin_alarm_show(struct device *dev,
Chris Verges2bf92332013-01-05 01:41:20 -0800147 struct device_attribute *da, char *buf)
148{
Chris Verges2bf92332013-01-05 01:41:20 -0800149 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck37102632013-09-01 18:32:52 -0700150 struct lm73_data *data = dev_get_drvdata(dev);
Chris Verges2bf92332013-01-05 01:41:20 -0800151 s32 ctrl;
152
153 mutex_lock(&data->lock);
Guenter Roeck37102632013-09-01 18:32:52 -0700154 ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
Chris Verges2bf92332013-01-05 01:41:20 -0800155 if (ctrl < 0)
156 goto abort;
157 data->ctrl = ctrl;
158 mutex_unlock(&data->lock);
159
160 return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
161
162abort:
163 mutex_unlock(&data->lock);
164 return ctrl;
165}
166
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100167/*-----------------------------------------------------------------------*/
168
169/* sysfs attributes for hwmon */
170
Guenter Roeck0f875ac2018-12-10 14:02:11 -0800171static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX);
172static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN);
173static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT);
174static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0);
175static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm,
176 LM73_CTRL_HI_SHIFT);
177static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm,
178 LM73_CTRL_LO_SHIFT);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100179
Guenter Roeck37102632013-09-01 18:32:52 -0700180static struct attribute *lm73_attrs[] = {
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100181 &sensor_dev_attr_temp1_input.dev_attr.attr,
182 &sensor_dev_attr_temp1_max.dev_attr.attr,
183 &sensor_dev_attr_temp1_min.dev_attr.attr,
Chris Verges8c14d122013-01-05 01:41:19 -0800184 &sensor_dev_attr_update_interval.dev_attr.attr,
Chris Verges2bf92332013-01-05 01:41:20 -0800185 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
186 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100187 NULL
188};
Guenter Roeck37102632013-09-01 18:32:52 -0700189ATTRIBUTE_GROUPS(lm73);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100190
191/*-----------------------------------------------------------------------*/
192
193/* device probe and removal */
194
195static int
196lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
197{
Guenter Roeck37102632013-09-01 18:32:52 -0700198 struct device *dev = &client->dev;
199 struct device *hwmon_dev;
Chris Verges8c14d122013-01-05 01:41:19 -0800200 struct lm73_data *data;
201 int ctrl;
202
Guenter Roeck37102632013-09-01 18:32:52 -0700203 data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
Chris Verges8c14d122013-01-05 01:41:19 -0800204 if (!data)
205 return -ENOMEM;
206
Guenter Roeck37102632013-09-01 18:32:52 -0700207 data->client = client;
Chris Verges8c14d122013-01-05 01:41:19 -0800208 mutex_init(&data->lock);
209
210 ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
211 if (ctrl < 0)
212 return ctrl;
213 data->ctrl = ctrl;
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100214
Guenter Roeck37102632013-09-01 18:32:52 -0700215 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
216 data, lm73_groups);
217 if (IS_ERR(hwmon_dev))
218 return PTR_ERR(hwmon_dev);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100219
Guenter Roeck37102632013-09-01 18:32:52 -0700220 dev_info(dev, "sensor '%s'\n", client->name);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100221
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100222 return 0;
223}
224
225static const struct i2c_device_id lm73_ids[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100226 { "lm73", 0 },
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100227 { /* LIST END */ }
228};
229MODULE_DEVICE_TABLE(i2c, lm73_ids);
230
231/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100232static int lm73_detect(struct i2c_client *new_client,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100233 struct i2c_board_info *info)
234{
235 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100236 int id, ctrl, conf;
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100237
238 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
239 I2C_FUNC_SMBUS_WORD_DATA))
240 return -ENODEV;
241
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100242 /*
243 * Do as much detection as possible with byte reads first, as word
244 * reads can confuse other devices.
245 */
246 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
247 if (ctrl < 0 || (ctrl & 0x10))
248 return -ENODEV;
249
250 conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
251 if (conf < 0 || (conf & 0x0c))
252 return -ENODEV;
253
254 id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
255 if (id < 0 || id != (LM73_ID & 0xff))
256 return -ENODEV;
257
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100258 /* Check device ID */
259 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100260 if (id < 0 || id != LM73_ID)
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100261 return -ENODEV;
262
263 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
264
265 return 0;
266}
267
268static struct i2c_driver lm73_driver = {
269 .class = I2C_CLASS_HWMON,
270 .driver = {
271 .name = "lm73",
272 },
273 .probe = lm73_probe,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100274 .id_table = lm73_ids,
275 .detect = lm73_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100276 .address_list = normal_i2c,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100277};
278
Axel Linf0967ee2012-01-20 15:38:18 +0800279module_i2c_driver(lm73_driver);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100280
281MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
282MODULE_DESCRIPTION("LM73 driver");
283MODULE_LICENSE("GPL");