blob: 9e64d96620d39f9087f6ebd71566d6f6f1594afb [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>
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>
Adrien Demarez4e233cb2009-12-09 20:35:50 +010019#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
Adrien Demarez4e233cb2009-12-09 20:35:50 +010029/* 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
Jean Delvare90f41022011-11-04 12:00:47 +010037#define LM73_ID 0x9001 /* 0x0190, byte-swapped */
Adrien Demarez4e233cb2009-12-09 20:35:50 +010038#define DRVNAME "lm73"
39#define LM73_TEMP_MIN (-40)
40#define LM73_TEMP_MAX 150
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;
52
53 int status = strict_strtol(buf, 10, &temp);
54 if (status < 0)
55 return status;
56
57 /* Write value */
58 value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
59 (LM73_TEMP_MAX*4)) << 5;
Jean Delvare90f41022011-11-04 12:00:47 +010060 i2c_smbus_write_word_swapped(client, attr->index, value);
Adrien Demarez4e233cb2009-12-09 20:35:50 +010061 return count;
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);
69 /* use integer division instead of equivalent right shift to
70 guarantee arithmetic shift and preserve the sign */
Jean Delvare90f41022011-11-04 12:00:47 +010071 int temp = ((s16) (i2c_smbus_read_word_swapped(client,
72 attr->index))*250) / 32;
Adrien Demarez4e233cb2009-12-09 20:35:50 +010073 return sprintf(buf, "%d\n", temp);
74}
75
76
77/*-----------------------------------------------------------------------*/
78
79/* sysfs attributes for hwmon */
80
81static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
82 show_temp, set_temp, LM73_REG_MAX);
83static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
84 show_temp, set_temp, LM73_REG_MIN);
85static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
86 show_temp, NULL, LM73_REG_INPUT);
87
88
89static struct attribute *lm73_attributes[] = {
90 &sensor_dev_attr_temp1_input.dev_attr.attr,
91 &sensor_dev_attr_temp1_max.dev_attr.attr,
92 &sensor_dev_attr_temp1_min.dev_attr.attr,
93
94 NULL
95};
96
97static const struct attribute_group lm73_group = {
98 .attrs = lm73_attributes,
99};
100
101/*-----------------------------------------------------------------------*/
102
103/* device probe and removal */
104
105static int
106lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
107{
108 struct device *hwmon_dev;
109 int status;
110
111 /* Register sysfs hooks */
112 status = sysfs_create_group(&client->dev.kobj, &lm73_group);
113 if (status)
114 return status;
115
116 hwmon_dev = hwmon_device_register(&client->dev);
117 if (IS_ERR(hwmon_dev)) {
118 status = PTR_ERR(hwmon_dev);
119 goto exit_remove;
120 }
121 i2c_set_clientdata(client, hwmon_dev);
122
123 dev_info(&client->dev, "%s: sensor '%s'\n",
124 dev_name(hwmon_dev), client->name);
125
126 return 0;
127
128exit_remove:
129 sysfs_remove_group(&client->dev.kobj, &lm73_group);
130 return status;
131}
132
133static int lm73_remove(struct i2c_client *client)
134{
135 struct device *hwmon_dev = i2c_get_clientdata(client);
136
137 hwmon_device_unregister(hwmon_dev);
138 sysfs_remove_group(&client->dev.kobj, &lm73_group);
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100139 return 0;
140}
141
142static const struct i2c_device_id lm73_ids[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100143 { "lm73", 0 },
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100144 { /* LIST END */ }
145};
146MODULE_DEVICE_TABLE(i2c, lm73_ids);
147
148/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100149static int lm73_detect(struct i2c_client *new_client,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100150 struct i2c_board_info *info)
151{
152 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100153 int id, ctrl, conf;
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100154
155 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
156 I2C_FUNC_SMBUS_WORD_DATA))
157 return -ENODEV;
158
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100159 /*
160 * Do as much detection as possible with byte reads first, as word
161 * reads can confuse other devices.
162 */
163 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
164 if (ctrl < 0 || (ctrl & 0x10))
165 return -ENODEV;
166
167 conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
168 if (conf < 0 || (conf & 0x0c))
169 return -ENODEV;
170
171 id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
172 if (id < 0 || id != (LM73_ID & 0xff))
173 return -ENODEV;
174
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100175 /* Check device ID */
176 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
Jean Delvare24d6e2a2011-11-04 12:00:46 +0100177 if (id < 0 || id != LM73_ID)
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100178 return -ENODEV;
179
180 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
181
182 return 0;
183}
184
185static struct i2c_driver lm73_driver = {
186 .class = I2C_CLASS_HWMON,
187 .driver = {
188 .name = "lm73",
189 },
190 .probe = lm73_probe,
191 .remove = lm73_remove,
192 .id_table = lm73_ids,
193 .detect = lm73_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100194 .address_list = normal_i2c,
Adrien Demarez4e233cb2009-12-09 20:35:50 +0100195};
196
197/* module glue */
198
199static int __init sensors_lm73_init(void)
200{
201 return i2c_add_driver(&lm73_driver);
202}
203
204static void __exit sensors_lm73_exit(void)
205{
206 i2c_del_driver(&lm73_driver);
207}
208
209MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
210MODULE_DESCRIPTION("LM73 driver");
211MODULE_LICENSE("GPL");
212
213module_init(sensors_lm73_init);
214module_exit(sensors_lm73_exit);