i2c: Get rid of struct i2c_client_address_data
[linux-2.6/btrfs-unstable.git] / drivers / hwmon / lm73.c
blobfb6ab9a9a60880c0ead94d52d2521548b0d36cde
1 /*
2 * LM73 Sensor driver
3 * Based on LM75
5 * Copyright (C) 2007, CenoSYS (www.cenosys.com).
6 * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
8 * Guillaume Ligneul <guillaume.ligneul@gmail.com>
9 * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
10 * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
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
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>
26 /* Addresses scanned */
27 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
28 0x4d, 0x4e, I2C_CLIENT_END };
30 /* Insmod parameters */
31 I2C_CLIENT_INSMOD_1(lm73);
33 /* LM73 registers */
34 #define LM73_REG_INPUT 0x00
35 #define LM73_REG_CONF 0x01
36 #define LM73_REG_MAX 0x02
37 #define LM73_REG_MIN 0x03
38 #define LM73_REG_CTRL 0x04
39 #define LM73_REG_ID 0x07
41 #define LM73_ID 0x9001 /* or 0x190 after a swab16() */
42 #define DRVNAME "lm73"
43 #define LM73_TEMP_MIN (-40)
44 #define LM73_TEMP_MAX 150
46 /*-----------------------------------------------------------------------*/
49 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
50 const char *buf, size_t count)
52 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
53 struct i2c_client *client = to_i2c_client(dev);
54 long temp;
55 short value;
57 int status = strict_strtol(buf, 10, &temp);
58 if (status < 0)
59 return status;
61 /* Write value */
62 value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
63 (LM73_TEMP_MAX*4)) << 5;
64 i2c_smbus_write_word_data(client, attr->index, swab16(value));
65 return count;
68 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
69 char *buf)
71 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
72 struct i2c_client *client = to_i2c_client(dev);
73 /* use integer division instead of equivalent right shift to
74 guarantee arithmetic shift and preserve the sign */
75 int temp = ((s16) (swab16(i2c_smbus_read_word_data(client,
76 attr->index)))*250) / 32;
77 return sprintf(buf, "%d\n", temp);
81 /*-----------------------------------------------------------------------*/
83 /* sysfs attributes for hwmon */
85 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
86 show_temp, set_temp, LM73_REG_MAX);
87 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
88 show_temp, set_temp, LM73_REG_MIN);
89 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
90 show_temp, NULL, LM73_REG_INPUT);
93 static struct attribute *lm73_attributes[] = {
94 &sensor_dev_attr_temp1_input.dev_attr.attr,
95 &sensor_dev_attr_temp1_max.dev_attr.attr,
96 &sensor_dev_attr_temp1_min.dev_attr.attr,
98 NULL
101 static const struct attribute_group lm73_group = {
102 .attrs = lm73_attributes,
105 /*-----------------------------------------------------------------------*/
107 /* device probe and removal */
109 static int
110 lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
112 struct device *hwmon_dev;
113 int status;
115 /* Register sysfs hooks */
116 status = sysfs_create_group(&client->dev.kobj, &lm73_group);
117 if (status)
118 return status;
120 hwmon_dev = hwmon_device_register(&client->dev);
121 if (IS_ERR(hwmon_dev)) {
122 status = PTR_ERR(hwmon_dev);
123 goto exit_remove;
125 i2c_set_clientdata(client, hwmon_dev);
127 dev_info(&client->dev, "%s: sensor '%s'\n",
128 dev_name(hwmon_dev), client->name);
130 return 0;
132 exit_remove:
133 sysfs_remove_group(&client->dev.kobj, &lm73_group);
134 return status;
137 static int lm73_remove(struct i2c_client *client)
139 struct device *hwmon_dev = i2c_get_clientdata(client);
141 hwmon_device_unregister(hwmon_dev);
142 sysfs_remove_group(&client->dev.kobj, &lm73_group);
143 i2c_set_clientdata(client, NULL);
144 return 0;
147 static const struct i2c_device_id lm73_ids[] = {
148 { "lm73", lm73 },
149 { /* LIST END */ }
151 MODULE_DEVICE_TABLE(i2c, lm73_ids);
153 /* Return 0 if detection is successful, -ENODEV otherwise */
154 static int lm73_detect(struct i2c_client *new_client,
155 struct i2c_board_info *info)
157 struct i2c_adapter *adapter = new_client->adapter;
158 u16 id;
159 u8 ctrl;
161 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
162 I2C_FUNC_SMBUS_WORD_DATA))
163 return -ENODEV;
165 /* Check device ID */
166 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
167 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
168 if ((id != LM73_ID) || (ctrl & 0x10))
169 return -ENODEV;
171 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
173 return 0;
176 static struct i2c_driver lm73_driver = {
177 .class = I2C_CLASS_HWMON,
178 .driver = {
179 .name = "lm73",
181 .probe = lm73_probe,
182 .remove = lm73_remove,
183 .id_table = lm73_ids,
184 .detect = lm73_detect,
185 .address_list = normal_i2c,
188 /* module glue */
190 static int __init sensors_lm73_init(void)
192 return i2c_add_driver(&lm73_driver);
195 static void __exit sensors_lm73_exit(void)
197 i2c_del_driver(&lm73_driver);
200 MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
201 MODULE_DESCRIPTION("LM73 driver");
202 MODULE_LICENSE("GPL");
204 module_init(sensors_lm73_init);
205 module_exit(sensors_lm73_exit);