hwmon: (lm73) Fix lower and upper temperature limits
[deliverable/linux.git] / drivers / hwmon / lm73.c
CommitLineData
4e233cbe
AD
1/*
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>
11 *
12 * This software program is licensed subject to the GNU General Public License
13 * (GPL).Version 2,June 1991, available at
14 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
4e233cbe
AD
19#include <linux/i2c.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
22#include <linux/err.h>
23
24
25/* Addresses scanned */
26static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
27 0x4d, 0x4e, I2C_CLIENT_END };
28
4e233cbe
AD
29/* LM73 registers */
30#define LM73_REG_INPUT 0x00
31#define LM73_REG_CONF 0x01
32#define LM73_REG_MAX 0x02
33#define LM73_REG_MIN 0x03
34#define LM73_REG_CTRL 0x04
35#define LM73_REG_ID 0x07
36
90f4102c 37#define LM73_ID 0x9001 /* 0x0190, byte-swapped */
4e233cbe 38#define DRVNAME "lm73"
91bba688
GR
39#define LM73_TEMP_MIN (-256000 / 250)
40#define LM73_TEMP_MAX (255750 / 250)
4e233cbe
AD
41
42/*-----------------------------------------------------------------------*/
43
44
45static ssize_t set_temp(struct device *dev, struct device_attribute *da,
46 const char *buf, size_t count)
47{
48 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
49 struct i2c_client *client = to_i2c_client(dev);
50 long temp;
51 short value;
0602934f 52 s32 err;
4e233cbe 53
179c4fdb 54 int status = kstrtol(buf, 10, &temp);
4e233cbe
AD
55 if (status < 0)
56 return status;
57
58 /* Write value */
91bba688 59 value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
0602934f
CV
60 err = i2c_smbus_write_word_swapped(client, attr->index, value);
61 return (err < 0) ? err : count;
4e233cbe
AD
62}
63
64static ssize_t show_temp(struct device *dev, struct device_attribute *da,
65 char *buf)
66{
67 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
68 struct i2c_client *client = to_i2c_client(dev);
0602934f
CV
69 int temp;
70
71 s32 err = i2c_smbus_read_word_swapped(client, attr->index);
72 if (err < 0)
73 return err;
74
4e233cbe
AD
75 /* use integer division instead of equivalent right shift to
76 guarantee arithmetic shift and preserve the sign */
0602934f
CV
77 temp = (((s16) err) * 250) / 32;
78 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
4e233cbe
AD
79}
80
81
82/*-----------------------------------------------------------------------*/
83
84/* sysfs attributes for hwmon */
85
86static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
87 show_temp, set_temp, LM73_REG_MAX);
88static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
89 show_temp, set_temp, LM73_REG_MIN);
90static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
91 show_temp, NULL, LM73_REG_INPUT);
92
93
94static struct attribute *lm73_attributes[] = {
95 &sensor_dev_attr_temp1_input.dev_attr.attr,
96 &sensor_dev_attr_temp1_max.dev_attr.attr,
97 &sensor_dev_attr_temp1_min.dev_attr.attr,
98
99 NULL
100};
101
102static const struct attribute_group lm73_group = {
103 .attrs = lm73_attributes,
104};
105
106/*-----------------------------------------------------------------------*/
107
108/* device probe and removal */
109
110static int
111lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
112{
113 struct device *hwmon_dev;
114 int status;
115
116 /* Register sysfs hooks */
117 status = sysfs_create_group(&client->dev.kobj, &lm73_group);
118 if (status)
119 return status;
120
121 hwmon_dev = hwmon_device_register(&client->dev);
122 if (IS_ERR(hwmon_dev)) {
123 status = PTR_ERR(hwmon_dev);
124 goto exit_remove;
125 }
126 i2c_set_clientdata(client, hwmon_dev);
127
128 dev_info(&client->dev, "%s: sensor '%s'\n",
129 dev_name(hwmon_dev), client->name);
130
131 return 0;
132
133exit_remove:
134 sysfs_remove_group(&client->dev.kobj, &lm73_group);
135 return status;
136}
137
138static int lm73_remove(struct i2c_client *client)
139{
140 struct device *hwmon_dev = i2c_get_clientdata(client);
141
142 hwmon_device_unregister(hwmon_dev);
143 sysfs_remove_group(&client->dev.kobj, &lm73_group);
4e233cbe
AD
144 return 0;
145}
146
147static const struct i2c_device_id lm73_ids[] = {
1f86df49 148 { "lm73", 0 },
4e233cbe
AD
149 { /* LIST END */ }
150};
151MODULE_DEVICE_TABLE(i2c, lm73_ids);
152
153/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 154static int lm73_detect(struct i2c_client *new_client,
4e233cbe
AD
155 struct i2c_board_info *info)
156{
157 struct i2c_adapter *adapter = new_client->adapter;
24d6e2a8 158 int id, ctrl, conf;
4e233cbe
AD
159
160 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
161 I2C_FUNC_SMBUS_WORD_DATA))
162 return -ENODEV;
163
24d6e2a8
JD
164 /*
165 * Do as much detection as possible with byte reads first, as word
166 * reads can confuse other devices.
167 */
168 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
169 if (ctrl < 0 || (ctrl & 0x10))
170 return -ENODEV;
171
172 conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
173 if (conf < 0 || (conf & 0x0c))
174 return -ENODEV;
175
176 id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
177 if (id < 0 || id != (LM73_ID & 0xff))
178 return -ENODEV;
179
4e233cbe
AD
180 /* Check device ID */
181 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
24d6e2a8 182 if (id < 0 || id != LM73_ID)
4e233cbe
AD
183 return -ENODEV;
184
185 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
186
187 return 0;
188}
189
190static struct i2c_driver lm73_driver = {
191 .class = I2C_CLASS_HWMON,
192 .driver = {
193 .name = "lm73",
194 },
195 .probe = lm73_probe,
196 .remove = lm73_remove,
197 .id_table = lm73_ids,
198 .detect = lm73_detect,
c3813d6a 199 .address_list = normal_i2c,
4e233cbe
AD
200};
201
f0967eea 202module_i2c_driver(lm73_driver);
4e233cbe
AD
203
204MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
205MODULE_DESCRIPTION("LM73 driver");
206MODULE_LICENSE("GPL");
This page took 0.234056 seconds and 5 git commands to generate.