]>
Commit | Line | Data |
---|---|---|
9410700b AP |
1 | /* tmp421.c |
2 | * | |
3 | * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de> | |
4 | * Preliminary support by: | |
5 | * Melvin Rook, Raymond Ng | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC. | |
24 | * Supported models: TMP421, TMP422, TMP423 | |
25 | */ | |
26 | ||
27 | #include <linux/module.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/jiffies.h> | |
31 | #include <linux/i2c.h> | |
32 | #include <linux/hwmon.h> | |
33 | #include <linux/hwmon-sysfs.h> | |
34 | #include <linux/err.h> | |
35 | #include <linux/mutex.h> | |
36 | #include <linux/sysfs.h> | |
37 | ||
38 | /* Addresses to scan */ | |
918ee91c JD |
39 | static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f, |
40 | I2C_CLIENT_END }; | |
9410700b | 41 | |
e5e9f44c | 42 | enum chips { tmp421, tmp422, tmp423 }; |
9410700b AP |
43 | |
44 | /* The TMP421 registers */ | |
a63ee9d8 | 45 | #define TMP421_STATUS_REG 0x08 |
9410700b AP |
46 | #define TMP421_CONFIG_REG_1 0x09 |
47 | #define TMP421_CONVERSION_RATE_REG 0x0B | |
48 | #define TMP421_MANUFACTURER_ID_REG 0xFE | |
49 | #define TMP421_DEVICE_ID_REG 0xFF | |
50 | ||
51 | static const u8 TMP421_TEMP_MSB[4] = { 0x00, 0x01, 0x02, 0x03 }; | |
52 | static const u8 TMP421_TEMP_LSB[4] = { 0x10, 0x11, 0x12, 0x13 }; | |
53 | ||
54 | /* Flags */ | |
55 | #define TMP421_CONFIG_SHUTDOWN 0x40 | |
56 | #define TMP421_CONFIG_RANGE 0x04 | |
57 | ||
58 | /* Manufacturer / Device ID's */ | |
59 | #define TMP421_MANUFACTURER_ID 0x55 | |
60 | #define TMP421_DEVICE_ID 0x21 | |
61 | #define TMP422_DEVICE_ID 0x22 | |
62 | #define TMP423_DEVICE_ID 0x23 | |
63 | ||
64 | static const struct i2c_device_id tmp421_id[] = { | |
8d59582a JD |
65 | { "tmp421", 2 }, |
66 | { "tmp422", 3 }, | |
67 | { "tmp423", 4 }, | |
9410700b AP |
68 | { } |
69 | }; | |
70 | MODULE_DEVICE_TABLE(i2c, tmp421_id); | |
71 | ||
72 | struct tmp421_data { | |
276dac80 | 73 | struct i2c_client *client; |
9410700b AP |
74 | struct mutex update_lock; |
75 | char valid; | |
76 | unsigned long last_updated; | |
8d59582a | 77 | int channels; |
9410700b AP |
78 | u8 config; |
79 | s16 temp[4]; | |
80 | }; | |
81 | ||
82 | static int temp_from_s16(s16 reg) | |
83 | { | |
a44908d7 JD |
84 | /* Mask out status bits */ |
85 | int temp = reg & ~0xf; | |
9410700b AP |
86 | |
87 | return (temp * 1000 + 128) / 256; | |
88 | } | |
89 | ||
90 | static int temp_from_u16(u16 reg) | |
91 | { | |
a44908d7 JD |
92 | /* Mask out status bits */ |
93 | int temp = reg & ~0xf; | |
9410700b AP |
94 | |
95 | /* Add offset for extended temperature range. */ | |
96 | temp -= 64 * 256; | |
97 | ||
98 | return (temp * 1000 + 128) / 256; | |
99 | } | |
100 | ||
101 | static struct tmp421_data *tmp421_update_device(struct device *dev) | |
102 | { | |
276dac80 GR |
103 | struct tmp421_data *data = dev_get_drvdata(dev); |
104 | struct i2c_client *client = data->client; | |
9410700b AP |
105 | int i; |
106 | ||
107 | mutex_lock(&data->update_lock); | |
108 | ||
109 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { | |
110 | data->config = i2c_smbus_read_byte_data(client, | |
111 | TMP421_CONFIG_REG_1); | |
112 | ||
8d59582a | 113 | for (i = 0; i < data->channels; i++) { |
9410700b AP |
114 | data->temp[i] = i2c_smbus_read_byte_data(client, |
115 | TMP421_TEMP_MSB[i]) << 8; | |
116 | data->temp[i] |= i2c_smbus_read_byte_data(client, | |
117 | TMP421_TEMP_LSB[i]); | |
118 | } | |
119 | data->last_updated = jiffies; | |
120 | data->valid = 1; | |
121 | } | |
122 | ||
123 | mutex_unlock(&data->update_lock); | |
124 | ||
125 | return data; | |
126 | } | |
127 | ||
128 | static ssize_t show_temp_value(struct device *dev, | |
129 | struct device_attribute *devattr, char *buf) | |
130 | { | |
131 | int index = to_sensor_dev_attr(devattr)->index; | |
132 | struct tmp421_data *data = tmp421_update_device(dev); | |
133 | int temp; | |
134 | ||
135 | mutex_lock(&data->update_lock); | |
136 | if (data->config & TMP421_CONFIG_RANGE) | |
137 | temp = temp_from_u16(data->temp[index]); | |
138 | else | |
139 | temp = temp_from_s16(data->temp[index]); | |
140 | mutex_unlock(&data->update_lock); | |
141 | ||
142 | return sprintf(buf, "%d\n", temp); | |
143 | } | |
144 | ||
145 | static ssize_t show_fault(struct device *dev, | |
146 | struct device_attribute *devattr, char *buf) | |
147 | { | |
148 | int index = to_sensor_dev_attr(devattr)->index; | |
149 | struct tmp421_data *data = tmp421_update_device(dev); | |
150 | ||
151 | /* | |
152 | * The OPEN bit signals a fault. This is bit 0 of the temperature | |
153 | * register (low byte). | |
154 | */ | |
155 | if (data->temp[index] & 0x01) | |
156 | return sprintf(buf, "1\n"); | |
157 | else | |
158 | return sprintf(buf, "0\n"); | |
159 | } | |
160 | ||
587a1f16 | 161 | static umode_t tmp421_is_visible(struct kobject *kobj, struct attribute *a, |
9410700b AP |
162 | int n) |
163 | { | |
164 | struct device *dev = container_of(kobj, struct device, kobj); | |
165 | struct tmp421_data *data = dev_get_drvdata(dev); | |
166 | struct device_attribute *devattr; | |
167 | unsigned int index; | |
168 | ||
169 | devattr = container_of(a, struct device_attribute, attr); | |
170 | index = to_sensor_dev_attr(devattr)->index; | |
171 | ||
8d59582a | 172 | if (index < data->channels) |
9410700b AP |
173 | return a->mode; |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0); | |
179 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1); | |
180 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1); | |
181 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2); | |
182 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2); | |
183 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3); | |
184 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3); | |
185 | ||
186 | static struct attribute *tmp421_attr[] = { | |
187 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
188 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
189 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | |
190 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
191 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | |
192 | &sensor_dev_attr_temp4_input.dev_attr.attr, | |
193 | &sensor_dev_attr_temp4_fault.dev_attr.attr, | |
194 | NULL | |
195 | }; | |
196 | ||
197 | static const struct attribute_group tmp421_group = { | |
198 | .attrs = tmp421_attr, | |
199 | .is_visible = tmp421_is_visible, | |
200 | }; | |
201 | ||
276dac80 GR |
202 | static const struct attribute_group *tmp421_groups[] = { |
203 | &tmp421_group, | |
204 | NULL | |
205 | }; | |
206 | ||
9410700b AP |
207 | static int tmp421_init_client(struct i2c_client *client) |
208 | { | |
209 | int config, config_orig; | |
210 | ||
211 | /* Set the conversion rate to 2 Hz */ | |
212 | i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05); | |
213 | ||
214 | /* Start conversions (disable shutdown if necessary) */ | |
215 | config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1); | |
216 | if (config < 0) { | |
b55f3757 GR |
217 | dev_err(&client->dev, |
218 | "Could not read configuration register (%d)\n", config); | |
010a166a | 219 | return config; |
9410700b AP |
220 | } |
221 | ||
222 | config_orig = config; | |
223 | config &= ~TMP421_CONFIG_SHUTDOWN; | |
224 | ||
225 | if (config != config_orig) { | |
226 | dev_info(&client->dev, "Enable monitoring chip\n"); | |
227 | i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config); | |
228 | } | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
310ec792 | 233 | static int tmp421_detect(struct i2c_client *client, |
9410700b AP |
234 | struct i2c_board_info *info) |
235 | { | |
dbe73c8f | 236 | enum chips kind; |
9410700b AP |
237 | struct i2c_adapter *adapter = client->adapter; |
238 | const char *names[] = { "TMP421", "TMP422", "TMP423" }; | |
a63ee9d8 | 239 | int addr = client->addr; |
dbe73c8f | 240 | u8 reg; |
9410700b AP |
241 | |
242 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
243 | return -ENODEV; | |
244 | ||
dbe73c8f JD |
245 | reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG); |
246 | if (reg != TMP421_MANUFACTURER_ID) | |
247 | return -ENODEV; | |
248 | ||
a63ee9d8 GR |
249 | reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG); |
250 | if (reg & 0xf8) | |
251 | return -ENODEV; | |
252 | ||
253 | reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG); | |
254 | if (reg & 0x7f) | |
255 | return -ENODEV; | |
256 | ||
dbe73c8f JD |
257 | reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG); |
258 | switch (reg) { | |
259 | case TMP421_DEVICE_ID: | |
260 | kind = tmp421; | |
261 | break; | |
262 | case TMP422_DEVICE_ID: | |
a63ee9d8 GR |
263 | if (addr == 0x2a) |
264 | return -ENODEV; | |
dbe73c8f JD |
265 | kind = tmp422; |
266 | break; | |
267 | case TMP423_DEVICE_ID: | |
a63ee9d8 GR |
268 | if (addr != 0x4c && addr != 0x4d) |
269 | return -ENODEV; | |
dbe73c8f JD |
270 | kind = tmp423; |
271 | break; | |
272 | default: | |
273 | return -ENODEV; | |
9410700b | 274 | } |
dbe73c8f | 275 | |
dc71afe5 | 276 | strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE); |
9410700b | 277 | dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n", |
dc71afe5 | 278 | names[kind], client->addr); |
9410700b AP |
279 | |
280 | return 0; | |
281 | } | |
282 | ||
283 | static int tmp421_probe(struct i2c_client *client, | |
284 | const struct i2c_device_id *id) | |
285 | { | |
276dac80 GR |
286 | struct device *dev = &client->dev; |
287 | struct device *hwmon_dev; | |
9410700b AP |
288 | struct tmp421_data *data; |
289 | int err; | |
290 | ||
276dac80 | 291 | data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL); |
9410700b AP |
292 | if (!data) |
293 | return -ENOMEM; | |
294 | ||
9410700b | 295 | mutex_init(&data->update_lock); |
8d59582a | 296 | data->channels = id->driver_data; |
276dac80 | 297 | data->client = client; |
9410700b AP |
298 | |
299 | err = tmp421_init_client(client); | |
300 | if (err) | |
f625e0fd | 301 | return err; |
9410700b | 302 | |
276dac80 GR |
303 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
304 | data, tmp421_groups); | |
305 | return PTR_ERR_OR_ZERO(hwmon_dev); | |
9410700b AP |
306 | } |
307 | ||
308 | static struct i2c_driver tmp421_driver = { | |
309 | .class = I2C_CLASS_HWMON, | |
310 | .driver = { | |
311 | .name = "tmp421", | |
312 | }, | |
313 | .probe = tmp421_probe, | |
9410700b AP |
314 | .id_table = tmp421_id, |
315 | .detect = tmp421_detect, | |
c3813d6a | 316 | .address_list = normal_i2c, |
9410700b AP |
317 | }; |
318 | ||
f0967eea | 319 | module_i2c_driver(tmp421_driver); |
9410700b AP |
320 | |
321 | MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>"); | |
b55f3757 | 322 | MODULE_DESCRIPTION("Texas Instruments TMP421/422/423 temperature sensor driver"); |
9410700b | 323 | MODULE_LICENSE("GPL"); |