blob: 3c2c48d904e658736605ee95e5b21f08cd1ccf98 [file] [log] [blame]
Krzysztof Heltadd77c62007-07-08 22:43:00 +02001/*
Guenter Roeck4d387df2012-01-14 22:33:01 -08002 * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
5 * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
6 * Philip Edelbrock <phil@netroedge.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
Krzysztof Heltadd77c62007-07-08 22:43:00 +020022
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/err.h>
30#include <linux/mutex.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020031#include <linux/jiffies.h>
Krzysztof Heltadd77c62007-07-08 22:43:00 +020032
33MODULE_LICENSE("GPL");
34
35/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050036static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Krzysztof Heltadd77c62007-07-08 22:43:00 +020037
38/* Insmod parameters */
Jean Delvaree5e9f442009-12-14 21:17:27 +010039enum chips { thmc50, adm1022 };
Jean Delvaref95f0b42009-12-09 20:35:59 +010040
41static unsigned short adm1022_temp3[16];
42static unsigned int adm1022_temp3_num;
43module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
44MODULE_PARM_DESC(adm1022_temp3, "List of adapter,address pairs "
Krzysztof Heltadd77c62007-07-08 22:43:00 +020045 "to enable 3rd temperature (ADM1022 only)");
46
47/* Many THMC50 constants specified below */
48
49/* The THMC50 registers */
50#define THMC50_REG_CONF 0x40
51#define THMC50_REG_COMPANY_ID 0x3E
52#define THMC50_REG_DIE_CODE 0x3F
53#define THMC50_REG_ANALOG_OUT 0x19
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -040054/*
Krzysztof Heltbba891c2007-09-09 17:35:34 +020055 * The mirror status register cannot be used as
56 * reading it does not clear alarms.
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -040057 */
Krzysztof Heltbba891c2007-09-09 17:35:34 +020058#define THMC50_REG_INTR 0x41
Krzysztof Heltadd77c62007-07-08 22:43:00 +020059
Tobias Klauser5910a9b2008-02-17 15:39:24 +010060static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
61static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
62static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
Krzysztof Helt84f768c2008-08-06 22:41:05 +020063static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
64static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
Krzysztof Heltadd77c62007-07-08 22:43:00 +020065
66#define THMC50_REG_CONF_nFANOFF 0x20
Krzysztof Helt84f768c2008-08-06 22:41:05 +020067#define THMC50_REG_CONF_PROGRAMMED 0x08
Krzysztof Heltadd77c62007-07-08 22:43:00 +020068
69/* Each client has this additional data */
70struct thmc50_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070071 struct device *hwmon_dev;
Krzysztof Heltadd77c62007-07-08 22:43:00 +020072
73 struct mutex update_lock;
74 enum chips type;
75 unsigned long last_updated; /* In jiffies */
76 char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
77 char valid; /* !=0 if following fields are valid */
78
79 /* Register values */
80 s8 temp_input[3];
81 s8 temp_max[3];
82 s8 temp_min[3];
Krzysztof Helt84f768c2008-08-06 22:41:05 +020083 s8 temp_critical[3];
Krzysztof Heltadd77c62007-07-08 22:43:00 +020084 u8 analog_out;
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -040085 u8 alarms;
Krzysztof Heltadd77c62007-07-08 22:43:00 +020086};
87
Jean Delvare310ec792009-12-14 21:17:23 +010088static int thmc50_detect(struct i2c_client *client,
Jean Delvareccf37482008-07-16 19:30:16 +020089 struct i2c_board_info *info);
90static int thmc50_probe(struct i2c_client *client,
91 const struct i2c_device_id *id);
92static int thmc50_remove(struct i2c_client *client);
Krzysztof Heltadd77c62007-07-08 22:43:00 +020093static void thmc50_init_client(struct i2c_client *client);
94static struct thmc50_data *thmc50_update_device(struct device *dev);
95
Jean Delvareccf37482008-07-16 19:30:16 +020096static const struct i2c_device_id thmc50_id[] = {
97 { "adm1022", adm1022 },
98 { "thmc50", thmc50 },
99 { }
100};
101MODULE_DEVICE_TABLE(i2c, thmc50_id);
102
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200103static struct i2c_driver thmc50_driver = {
Jean Delvareccf37482008-07-16 19:30:16 +0200104 .class = I2C_CLASS_HWMON,
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200105 .driver = {
106 .name = "thmc50",
107 },
Jean Delvareccf37482008-07-16 19:30:16 +0200108 .probe = thmc50_probe,
109 .remove = thmc50_remove,
110 .id_table = thmc50_id,
111 .detect = thmc50_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100112 .address_list = normal_i2c,
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200113};
114
115static ssize_t show_analog_out(struct device *dev,
116 struct device_attribute *attr, char *buf)
117{
118 struct thmc50_data *data = thmc50_update_device(dev);
119 return sprintf(buf, "%d\n", data->analog_out);
120}
121
122static ssize_t set_analog_out(struct device *dev,
123 struct device_attribute *attr,
124 const char *buf, size_t count)
125{
126 struct i2c_client *client = to_i2c_client(dev);
127 struct thmc50_data *data = i2c_get_clientdata(client);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200128 int config;
Guenter Roeck4d387df2012-01-14 22:33:01 -0800129 unsigned long tmp;
130 int err;
131
132 err = kstrtoul(buf, 10, &tmp);
133 if (err)
134 return err;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200135
136 mutex_lock(&data->update_lock);
137 data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
138 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
139 data->analog_out);
140
141 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
142 if (data->analog_out == 0)
143 config &= ~THMC50_REG_CONF_nFANOFF;
144 else
145 config |= THMC50_REG_CONF_nFANOFF;
146 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
147
148 mutex_unlock(&data->update_lock);
149 return count;
150}
151
152/* There is only one PWM mode = DC */
153static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
154 char *buf)
155{
156 return sprintf(buf, "0\n");
157}
158
159/* Temperatures */
160static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
161 char *buf)
162{
163 int nr = to_sensor_dev_attr(attr)->index;
164 struct thmc50_data *data = thmc50_update_device(dev);
165 return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
166}
167
168static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
169 char *buf)
170{
171 int nr = to_sensor_dev_attr(attr)->index;
172 struct thmc50_data *data = thmc50_update_device(dev);
173 return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
174}
175
176static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
177 const char *buf, size_t count)
178{
179 int nr = to_sensor_dev_attr(attr)->index;
180 struct i2c_client *client = to_i2c_client(dev);
181 struct thmc50_data *data = i2c_get_clientdata(client);
Guenter Roeck4d387df2012-01-14 22:33:01 -0800182 long val;
183 int err;
184
185 err = kstrtol(buf, 10, &val);
186 if (err)
187 return err;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200188
189 mutex_lock(&data->update_lock);
190 data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
191 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
192 data->temp_min[nr]);
193 mutex_unlock(&data->update_lock);
194 return count;
195}
196
197static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
198 char *buf)
199{
200 int nr = to_sensor_dev_attr(attr)->index;
201 struct thmc50_data *data = thmc50_update_device(dev);
202 return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
203}
204
205static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
206 const char *buf, size_t count)
207{
208 int nr = to_sensor_dev_attr(attr)->index;
209 struct i2c_client *client = to_i2c_client(dev);
210 struct thmc50_data *data = i2c_get_clientdata(client);
Guenter Roeck4d387df2012-01-14 22:33:01 -0800211 long val;
212 int err;
213
214 err = kstrtol(buf, 10, &val);
215 if (err)
216 return err;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200217
218 mutex_lock(&data->update_lock);
219 data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
220 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
221 data->temp_max[nr]);
222 mutex_unlock(&data->update_lock);
223 return count;
224}
225
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200226static ssize_t show_temp_critical(struct device *dev,
227 struct device_attribute *attr,
228 char *buf)
229{
230 int nr = to_sensor_dev_attr(attr)->index;
231 struct thmc50_data *data = thmc50_update_device(dev);
232 return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
233}
234
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -0400235static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
236 char *buf)
237{
238 int index = to_sensor_dev_attr(attr)->index;
239 struct thmc50_data *data = thmc50_update_device(dev);
240
241 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
242}
243
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200244#define temp_reg(offset) \
245static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
246 NULL, offset - 1); \
247static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
248 show_temp_min, set_temp_min, offset - 1); \
249static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200250 show_temp_max, set_temp_max, offset - 1); \
251static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO, \
252 show_temp_critical, NULL, offset - 1);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200253
254temp_reg(1);
255temp_reg(2);
256temp_reg(3);
257
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -0400258static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
259static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
260static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
261static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
262static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
263
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200264static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
265 set_analog_out, 0);
266static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
267
268static struct attribute *thmc50_attributes[] = {
269 &sensor_dev_attr_temp1_max.dev_attr.attr,
270 &sensor_dev_attr_temp1_min.dev_attr.attr,
271 &sensor_dev_attr_temp1_input.dev_attr.attr,
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200272 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -0400273 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200274 &sensor_dev_attr_temp2_max.dev_attr.attr,
275 &sensor_dev_attr_temp2_min.dev_attr.attr,
276 &sensor_dev_attr_temp2_input.dev_attr.attr,
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200277 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -0400278 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
279 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200280 &sensor_dev_attr_pwm1.dev_attr.attr,
281 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
282 NULL
283};
284
285static const struct attribute_group thmc50_group = {
286 .attrs = thmc50_attributes,
287};
288
289/* for ADM1022 3rd temperature mode */
Jean Delvare894c00c2007-09-18 18:44:42 +0200290static struct attribute *temp3_attributes[] = {
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200291 &sensor_dev_attr_temp3_max.dev_attr.attr,
292 &sensor_dev_attr_temp3_min.dev_attr.attr,
293 &sensor_dev_attr_temp3_input.dev_attr.attr,
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200294 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -0400295 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
296 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200297 NULL
298};
299
Jean Delvare894c00c2007-09-18 18:44:42 +0200300static const struct attribute_group temp3_group = {
301 .attrs = temp3_attributes,
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200302};
303
Jean Delvareccf37482008-07-16 19:30:16 +0200304/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100305static int thmc50_detect(struct i2c_client *client,
Jean Delvareccf37482008-07-16 19:30:16 +0200306 struct i2c_board_info *info)
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200307{
308 unsigned company;
309 unsigned revision;
310 unsigned config;
Jean Delvareccf37482008-07-16 19:30:16 +0200311 struct i2c_adapter *adapter = client->adapter;
Jean Delvarecc28a612007-09-18 18:48:16 +0200312 const char *type_name;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200313
314 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
315 pr_debug("thmc50: detect failed, "
316 "smbus byte data not supported!\n");
Jean Delvareccf37482008-07-16 19:30:16 +0200317 return -ENODEV;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200318 }
319
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200320 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
321 client->addr, i2c_adapter_id(client->adapter));
322
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200323 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
324 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
325 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
Jean Delvare52df6442009-12-09 20:35:57 +0100326 if (revision < 0xc0 || (config & 0x10))
327 return -ENODEV;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200328
Jean Delvare52df6442009-12-09 20:35:57 +0100329 if (company == 0x41) {
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200330 int id = i2c_adapter_id(client->adapter);
331 int i;
332
333 type_name = "adm1022";
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200334 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
335 if (adm1022_temp3[i] == id &&
Jean Delvareccf37482008-07-16 19:30:16 +0200336 adm1022_temp3[i + 1] == client->addr) {
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200337 /* enable 2nd remote temp */
Jean Delvareccf37482008-07-16 19:30:16 +0200338 config |= (1 << 7);
339 i2c_smbus_write_byte_data(client,
340 THMC50_REG_CONF,
341 config);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200342 break;
343 }
Jean Delvare52df6442009-12-09 20:35:57 +0100344 } else if (company == 0x49) {
Jean Delvarecc28a612007-09-18 18:48:16 +0200345 type_name = "thmc50";
Jean Delvare52df6442009-12-09 20:35:57 +0100346 } else {
347 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
348 return -ENODEV;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200349 }
Jean Delvare52df6442009-12-09 20:35:57 +0100350
Jean Delvarecc28a612007-09-18 18:48:16 +0200351 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
352 type_name, (revision >> 4) - 0xc, revision & 0xf);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200353
Jean Delvareccf37482008-07-16 19:30:16 +0200354 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200355
Jean Delvareccf37482008-07-16 19:30:16 +0200356 return 0;
357}
358
359static int thmc50_probe(struct i2c_client *client,
360 const struct i2c_device_id *id)
361{
362 struct thmc50_data *data;
363 int err;
364
Guenter Roeckbd91d3b2012-06-02 11:20:23 -0700365 data = devm_kzalloc(&client->dev, sizeof(struct thmc50_data),
366 GFP_KERNEL);
367 if (!data)
368 return -ENOMEM;
Jean Delvareccf37482008-07-16 19:30:16 +0200369
370 i2c_set_clientdata(client, data);
371 data->type = id->driver_data;
372 mutex_init(&data->update_lock);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200373
374 thmc50_init_client(client);
375
376 /* Register sysfs hooks */
Guenter Roeck4d387df2012-01-14 22:33:01 -0800377 err = sysfs_create_group(&client->dev.kobj, &thmc50_group);
378 if (err)
Guenter Roeckbd91d3b2012-06-02 11:20:23 -0700379 return err;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200380
381 /* Register ADM1022 sysfs hooks */
Guenter Roeck4d387df2012-01-14 22:33:01 -0800382 if (data->has_temp3) {
383 err = sysfs_create_group(&client->dev.kobj, &temp3_group);
384 if (err)
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200385 goto exit_remove_sysfs_thmc50;
Guenter Roeck4d387df2012-01-14 22:33:01 -0800386 }
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200387
388 /* Register a new directory entry with module sensors */
Tony Jones1beeffe2007-08-20 13:46:20 -0700389 data->hwmon_dev = hwmon_device_register(&client->dev);
390 if (IS_ERR(data->hwmon_dev)) {
391 err = PTR_ERR(data->hwmon_dev);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200392 goto exit_remove_sysfs;
393 }
394
395 return 0;
396
397exit_remove_sysfs:
Jean Delvare894c00c2007-09-18 18:44:42 +0200398 if (data->has_temp3)
399 sysfs_remove_group(&client->dev.kobj, &temp3_group);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200400exit_remove_sysfs_thmc50:
401 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200402 return err;
403}
404
Jean Delvareccf37482008-07-16 19:30:16 +0200405static int thmc50_remove(struct i2c_client *client)
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200406{
407 struct thmc50_data *data = i2c_get_clientdata(client);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200408
Tony Jones1beeffe2007-08-20 13:46:20 -0700409 hwmon_device_unregister(data->hwmon_dev);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200410 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
Jean Delvare894c00c2007-09-18 18:44:42 +0200411 if (data->has_temp3)
412 sysfs_remove_group(&client->dev.kobj, &temp3_group);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200413
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200414 return 0;
415}
416
417static void thmc50_init_client(struct i2c_client *client)
418{
419 struct thmc50_data *data = i2c_get_clientdata(client);
420 int config;
421
422 data->analog_out = i2c_smbus_read_byte_data(client,
423 THMC50_REG_ANALOG_OUT);
424 /* set up to at least 1 */
425 if (data->analog_out == 0) {
426 data->analog_out = 1;
427 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
428 data->analog_out);
429 }
430 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
431 config |= 0x1; /* start the chip if it is in standby mode */
Jean Delvareccf37482008-07-16 19:30:16 +0200432 if (data->type == adm1022 && (config & (1 << 7)))
433 data->has_temp3 = 1;
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200434 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
435}
436
437static struct thmc50_data *thmc50_update_device(struct device *dev)
438{
439 struct i2c_client *client = to_i2c_client(dev);
440 struct thmc50_data *data = i2c_get_clientdata(client);
441 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
442
443 mutex_lock(&data->update_lock);
444
445 if (time_after(jiffies, data->last_updated + timeout)
446 || !data->valid) {
447
448 int temps = data->has_temp3 ? 3 : 2;
449 int i;
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200450 int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
451
452 prog &= THMC50_REG_CONF_PROGRAMMED;
453
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200454 for (i = 0; i < temps; i++) {
455 data->temp_input[i] = i2c_smbus_read_byte_data(client,
456 THMC50_REG_TEMP[i]);
457 data->temp_max[i] = i2c_smbus_read_byte_data(client,
458 THMC50_REG_TEMP_MAX[i]);
459 data->temp_min[i] = i2c_smbus_read_byte_data(client,
460 THMC50_REG_TEMP_MIN[i]);
Krzysztof Helt84f768c2008-08-06 22:41:05 +0200461 data->temp_critical[i] =
462 i2c_smbus_read_byte_data(client,
463 prog ? THMC50_REG_TEMP_CRITICAL[i]
464 : THMC50_REG_TEMP_DEFAULT[i]);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200465 }
466 data->analog_out =
467 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
Krzysztof Heltdcf3b5f2007-08-22 14:05:22 -0400468 data->alarms =
Krzysztof Heltbba891c2007-09-09 17:35:34 +0200469 i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200470 data->last_updated = jiffies;
471 data->valid = 1;
472 }
473
474 mutex_unlock(&data->update_lock);
475
476 return data;
477}
478
Axel Linf0967ee2012-01-20 15:38:18 +0800479module_i2c_driver(thmc50_driver);
Krzysztof Heltadd77c62007-07-08 22:43:00 +0200480
481MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
482MODULE_DESCRIPTION("THMC50 driver");