Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6-block.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>
19#include <linux/slab.h>
20#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
4e233cbe
AD
30/* 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
38#define LM73_ID 0x9001 /* or 0x190 after a swab16() */
39#define DRVNAME "lm73"
40#define LM73_TEMP_MIN (-40)
41#define LM73_TEMP_MAX 150
42
43/*-----------------------------------------------------------------------*/
44
45
46static ssize_t set_temp(struct device *dev, struct device_attribute *da,
47 const char *buf, size_t count)
48{
49 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
50 struct i2c_client *client = to_i2c_client(dev);
51 long temp;
52 short value;
53
54 int status = strict_strtol(buf, 10, &temp);
55 if (status < 0)
56 return status;
57
58 /* Write value */
59 value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
60 (LM73_TEMP_MAX*4)) << 5;
61 i2c_smbus_write_word_data(client, attr->index, swab16(value));
62 return count;
63}
64
65static ssize_t show_temp(struct device *dev, struct device_attribute *da,
66 char *buf)
67{
68 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
69 struct i2c_client *client = to_i2c_client(dev);
70 /* use integer division instead of equivalent right shift to
71 guarantee arithmetic shift and preserve the sign */
72 int temp = ((s16) (swab16(i2c_smbus_read_word_data(client,
73 attr->index)))*250) / 32;
74 return sprintf(buf, "%d\n", temp);
75}
76
77
78/*-----------------------------------------------------------------------*/
79
80/* sysfs attributes for hwmon */
81
82static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
83 show_temp, set_temp, LM73_REG_MAX);
84static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
85 show_temp, set_temp, LM73_REG_MIN);
86static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
87 show_temp, NULL, LM73_REG_INPUT);
88
89
90static struct attribute *lm73_attributes[] = {
91 &sensor_dev_attr_temp1_input.dev_attr.attr,
92 &sensor_dev_attr_temp1_max.dev_attr.attr,
93 &sensor_dev_attr_temp1_min.dev_attr.attr,
94
95 NULL
96};
97
98static const struct attribute_group lm73_group = {
99 .attrs = lm73_attributes,
100};
101
102/*-----------------------------------------------------------------------*/
103
104/* device probe and removal */
105
106static int
107lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
108{
109 struct device *hwmon_dev;
110 int status;
111
112 /* Register sysfs hooks */
113 status = sysfs_create_group(&client->dev.kobj, &lm73_group);
114 if (status)
115 return status;
116
117 hwmon_dev = hwmon_device_register(&client->dev);
118 if (IS_ERR(hwmon_dev)) {
119 status = PTR_ERR(hwmon_dev);
120 goto exit_remove;
121 }
122 i2c_set_clientdata(client, hwmon_dev);
123
124 dev_info(&client->dev, "%s: sensor '%s'\n",
125 dev_name(hwmon_dev), client->name);
126
127 return 0;
128
129exit_remove:
130 sysfs_remove_group(&client->dev.kobj, &lm73_group);
131 return status;
132}
133
134static int lm73_remove(struct i2c_client *client)
135{
136 struct device *hwmon_dev = i2c_get_clientdata(client);
137
138 hwmon_device_unregister(hwmon_dev);
139 sysfs_remove_group(&client->dev.kobj, &lm73_group);
140 i2c_set_clientdata(client, NULL);
141 return 0;
142}
143
144static const struct i2c_device_id lm73_ids[] = {
1f86df49 145 { "lm73", 0 },
4e233cbe
AD
146 { /* LIST END */ }
147};
148MODULE_DEVICE_TABLE(i2c, lm73_ids);
149
150/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 151static int lm73_detect(struct i2c_client *new_client,
4e233cbe
AD
152 struct i2c_board_info *info)
153{
154 struct i2c_adapter *adapter = new_client->adapter;
155 u16 id;
156 u8 ctrl;
157
158 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
159 I2C_FUNC_SMBUS_WORD_DATA))
160 return -ENODEV;
161
162 /* Check device ID */
163 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
164 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
165 if ((id != LM73_ID) || (ctrl & 0x10))
166 return -ENODEV;
167
168 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
169
170 return 0;
171}
172
173static struct i2c_driver lm73_driver = {
174 .class = I2C_CLASS_HWMON,
175 .driver = {
176 .name = "lm73",
177 },
178 .probe = lm73_probe,
179 .remove = lm73_remove,
180 .id_table = lm73_ids,
181 .detect = lm73_detect,
c3813d6a 182 .address_list = normal_i2c,
4e233cbe
AD
183};
184
185/* module glue */
186
187static int __init sensors_lm73_init(void)
188{
189 return i2c_add_driver(&lm73_driver);
190}
191
192static void __exit sensors_lm73_exit(void)
193{
194 i2c_del_driver(&lm73_driver);
195}
196
197MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
198MODULE_DESCRIPTION("LM73 driver");
199MODULE_LICENSE("GPL");
200
201module_init(sensors_lm73_init);
202module_exit(sensors_lm73_exit);