Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
6 * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
7 * Philip Edelbrock <phil@netroedge.com>
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
14#include <linux/hwmon.h>
15#include <linux/hwmon-sysfs.h>
16#include <linux/err.h>
17#include <linux/mutex.h>
18#include <linux/jiffies.h>
19
20MODULE_LICENSE("GPL");
21
22/* Addresses to scan */
23static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
24
25/* Insmod parameters */
26enum chips { thmc50, adm1022 };
27
28static unsigned short adm1022_temp3[16];
29static unsigned int adm1022_temp3_num;
30module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
31MODULE_PARM_DESC(adm1022_temp3,
32 "List of adapter,address pairs to enable 3rd temperature (ADM1022 only)");
33
34/* Many THMC50 constants specified below */
35
36/* The THMC50 registers */
37#define THMC50_REG_CONF 0x40
38#define THMC50_REG_COMPANY_ID 0x3E
39#define THMC50_REG_DIE_CODE 0x3F
40#define THMC50_REG_ANALOG_OUT 0x19
41/*
42 * The mirror status register cannot be used as
43 * reading it does not clear alarms.
44 */
45#define THMC50_REG_INTR 0x41
46
47static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
48static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
49static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
50static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
51static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
52
53#define THMC50_REG_CONF_nFANOFF 0x20
54#define THMC50_REG_CONF_PROGRAMMED 0x08
55
56/* Each client has this additional data */
57struct thmc50_data {
58 struct i2c_client *client;
59 const struct attribute_group *groups[3];
60
61 struct mutex update_lock;
62 enum chips type;
63 unsigned long last_updated; /* In jiffies */
64 char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
65 bool valid; /* true if following fields are valid */
66
67 /* Register values */
68 s8 temp_input[3];
69 s8 temp_max[3];
70 s8 temp_min[3];
71 s8 temp_critical[3];
72 u8 analog_out;
73 u8 alarms;
74};
75
76static struct thmc50_data *thmc50_update_device(struct device *dev)
77{
78 struct thmc50_data *data = dev_get_drvdata(dev);
79 struct i2c_client *client = data->client;
80 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
81
82 mutex_lock(&data->update_lock);
83
84 if (time_after(jiffies, data->last_updated + timeout)
85 || !data->valid) {
86
87 int temps = data->has_temp3 ? 3 : 2;
88 int i;
89 int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
90
91 prog &= THMC50_REG_CONF_PROGRAMMED;
92
93 for (i = 0; i < temps; i++) {
94 data->temp_input[i] = i2c_smbus_read_byte_data(client,
95 THMC50_REG_TEMP[i]);
96 data->temp_max[i] = i2c_smbus_read_byte_data(client,
97 THMC50_REG_TEMP_MAX[i]);
98 data->temp_min[i] = i2c_smbus_read_byte_data(client,
99 THMC50_REG_TEMP_MIN[i]);
100 data->temp_critical[i] =
101 i2c_smbus_read_byte_data(client,
102 prog ? THMC50_REG_TEMP_CRITICAL[i]
103 : THMC50_REG_TEMP_DEFAULT[i]);
104 }
105 data->analog_out =
106 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
107 data->alarms =
108 i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
109 data->last_updated = jiffies;
110 data->valid = true;
111 }
112
113 mutex_unlock(&data->update_lock);
114
115 return data;
116}
117
118static ssize_t analog_out_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 struct thmc50_data *data = thmc50_update_device(dev);
122 return sprintf(buf, "%d\n", data->analog_out);
123}
124
125static ssize_t analog_out_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
128{
129 struct thmc50_data *data = dev_get_drvdata(dev);
130 struct i2c_client *client = data->client;
131 int config;
132 unsigned long tmp;
133 int err;
134
135 err = kstrtoul(buf, 10, &tmp);
136 if (err)
137 return err;
138
139 mutex_lock(&data->update_lock);
140 data->analog_out = clamp_val(tmp, 0, 255);
141 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
142 data->analog_out);
143
144 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
145 if (data->analog_out == 0)
146 config &= ~THMC50_REG_CONF_nFANOFF;
147 else
148 config |= THMC50_REG_CONF_nFANOFF;
149 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
150
151 mutex_unlock(&data->update_lock);
152 return count;
153}
154
155/* There is only one PWM mode = DC */
156static ssize_t pwm_mode_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 return sprintf(buf, "0\n");
160}
161
162/* Temperatures */
163static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
164 char *buf)
165{
166 int nr = to_sensor_dev_attr(attr)->index;
167 struct thmc50_data *data = thmc50_update_device(dev);
168 return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
169}
170
171static ssize_t temp_min_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
174 int nr = to_sensor_dev_attr(attr)->index;
175 struct thmc50_data *data = thmc50_update_device(dev);
176 return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
177}
178
179static ssize_t temp_min_store(struct device *dev,
180 struct device_attribute *attr, const char *buf,
181 size_t count)
182{
183 int nr = to_sensor_dev_attr(attr)->index;
184 struct thmc50_data *data = dev_get_drvdata(dev);
185 struct i2c_client *client = data->client;
186 long val;
187 int err;
188
189 err = kstrtol(buf, 10, &val);
190 if (err)
191 return err;
192
193 mutex_lock(&data->update_lock);
194 data->temp_min[nr] = clamp_val(val / 1000, -128, 127);
195 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
196 data->temp_min[nr]);
197 mutex_unlock(&data->update_lock);
198 return count;
199}
200
201static ssize_t temp_max_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
203{
204 int nr = to_sensor_dev_attr(attr)->index;
205 struct thmc50_data *data = thmc50_update_device(dev);
206 return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
207}
208
209static ssize_t temp_max_store(struct device *dev,
210 struct device_attribute *attr, const char *buf,
211 size_t count)
212{
213 int nr = to_sensor_dev_attr(attr)->index;
214 struct thmc50_data *data = dev_get_drvdata(dev);
215 struct i2c_client *client = data->client;
216 long val;
217 int err;
218
219 err = kstrtol(buf, 10, &val);
220 if (err)
221 return err;
222
223 mutex_lock(&data->update_lock);
224 data->temp_max[nr] = clamp_val(val / 1000, -128, 127);
225 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
226 data->temp_max[nr]);
227 mutex_unlock(&data->update_lock);
228 return count;
229}
230
231static ssize_t temp_critical_show(struct device *dev,
232 struct device_attribute *attr, char *buf)
233{
234 int nr = to_sensor_dev_attr(attr)->index;
235 struct thmc50_data *data = thmc50_update_device(dev);
236 return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
237}
238
239static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 int index = to_sensor_dev_attr(attr)->index;
243 struct thmc50_data *data = thmc50_update_device(dev);
244
245 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
246}
247
248static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
249static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
250static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
251static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp_critical, 0);
252static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
253static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
254static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
255static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp_critical, 1);
256static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
257static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
258static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
259static SENSOR_DEVICE_ATTR_RO(temp3_crit, temp_critical, 2);
260
261static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 0);
262static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
263static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 1);
264static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 7);
265static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
266
267static SENSOR_DEVICE_ATTR_RW(pwm1, analog_out, 0);
268static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
269
270static struct attribute *thmc50_attributes[] = {
271 &sensor_dev_attr_temp1_max.dev_attr.attr,
272 &sensor_dev_attr_temp1_min.dev_attr.attr,
273 &sensor_dev_attr_temp1_input.dev_attr.attr,
274 &sensor_dev_attr_temp1_crit.dev_attr.attr,
275 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
276 &sensor_dev_attr_temp2_max.dev_attr.attr,
277 &sensor_dev_attr_temp2_min.dev_attr.attr,
278 &sensor_dev_attr_temp2_input.dev_attr.attr,
279 &sensor_dev_attr_temp2_crit.dev_attr.attr,
280 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
281 &sensor_dev_attr_temp2_fault.dev_attr.attr,
282 &sensor_dev_attr_pwm1.dev_attr.attr,
283 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
284 NULL
285};
286
287static const struct attribute_group thmc50_group = {
288 .attrs = thmc50_attributes,
289};
290
291/* for ADM1022 3rd temperature mode */
292static struct attribute *temp3_attributes[] = {
293 &sensor_dev_attr_temp3_max.dev_attr.attr,
294 &sensor_dev_attr_temp3_min.dev_attr.attr,
295 &sensor_dev_attr_temp3_input.dev_attr.attr,
296 &sensor_dev_attr_temp3_crit.dev_attr.attr,
297 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
298 &sensor_dev_attr_temp3_fault.dev_attr.attr,
299 NULL
300};
301
302static const struct attribute_group temp3_group = {
303 .attrs = temp3_attributes,
304};
305
306/* Return 0 if detection is successful, -ENODEV otherwise */
307static int thmc50_detect(struct i2c_client *client,
308 struct i2c_board_info *info)
309{
310 unsigned company;
311 unsigned revision;
312 unsigned config;
313 struct i2c_adapter *adapter = client->adapter;
314 const char *type_name;
315
316 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
317 pr_debug("thmc50: detect failed, smbus byte data not supported!\n");
318 return -ENODEV;
319 }
320
321 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
322 client->addr, i2c_adapter_id(client->adapter));
323
324 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
325 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
326 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
327 if (revision < 0xc0 || (config & 0x10))
328 return -ENODEV;
329
330 if (company == 0x41) {
331 int id = i2c_adapter_id(client->adapter);
332 int i;
333
334 type_name = "adm1022";
335 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
336 if (adm1022_temp3[i] == id &&
337 adm1022_temp3[i + 1] == client->addr) {
338 /* enable 2nd remote temp */
339 config |= (1 << 7);
340 i2c_smbus_write_byte_data(client,
341 THMC50_REG_CONF,
342 config);
343 break;
344 }
345 } else if (company == 0x49) {
346 type_name = "thmc50";
347 } else {
348 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
349 return -ENODEV;
350 }
351
352 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
353 type_name, (revision >> 4) - 0xc, revision & 0xf);
354
355 strscpy(info->type, type_name, I2C_NAME_SIZE);
356
357 return 0;
358}
359
360static void thmc50_init_client(struct thmc50_data *data)
361{
362 struct i2c_client *client = data->client;
363 int config;
364
365 data->analog_out = i2c_smbus_read_byte_data(client,
366 THMC50_REG_ANALOG_OUT);
367 /* set up to at least 1 */
368 if (data->analog_out == 0) {
369 data->analog_out = 1;
370 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
371 data->analog_out);
372 }
373 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
374 config |= 0x1; /* start the chip if it is in standby mode */
375 if (data->type == adm1022 && (config & (1 << 7)))
376 data->has_temp3 = 1;
377 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
378}
379
380static const struct i2c_device_id thmc50_id[];
381
382static int thmc50_probe(struct i2c_client *client)
383{
384 struct device *dev = &client->dev;
385 struct thmc50_data *data;
386 struct device *hwmon_dev;
387 int idx = 0;
388
389 data = devm_kzalloc(dev, sizeof(struct thmc50_data), GFP_KERNEL);
390 if (!data)
391 return -ENOMEM;
392
393 data->client = client;
394 data->type = i2c_match_id(thmc50_id, client)->driver_data;
395 mutex_init(&data->update_lock);
396
397 thmc50_init_client(data);
398
399 /* sysfs hooks */
400 data->groups[idx++] = &thmc50_group;
401
402 /* Register additional ADM1022 sysfs hooks */
403 if (data->has_temp3)
404 data->groups[idx++] = &temp3_group;
405
406 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
407 data, data->groups);
408 return PTR_ERR_OR_ZERO(hwmon_dev);
409}
410
411static const struct i2c_device_id thmc50_id[] = {
412 { "adm1022", adm1022 },
413 { "thmc50", thmc50 },
414 { }
415};
416MODULE_DEVICE_TABLE(i2c, thmc50_id);
417
418static struct i2c_driver thmc50_driver = {
419 .class = I2C_CLASS_HWMON,
420 .driver = {
421 .name = "thmc50",
422 },
423 .probe = thmc50_probe,
424 .id_table = thmc50_id,
425 .detect = thmc50_detect,
426 .address_list = normal_i2c,
427};
428
429module_i2c_driver(thmc50_driver);
430
431MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
432MODULE_DESCRIPTION("THMC50 driver");
1/*
2 * 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 */
22
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>
31#include <linux/jiffies.h>
32
33MODULE_LICENSE("GPL");
34
35/* Addresses to scan */
36static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37
38/* Insmod parameters */
39enum chips { thmc50, adm1022 };
40
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,
45 "List of adapter,address pairs 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
54/*
55 * The mirror status register cannot be used as
56 * reading it does not clear alarms.
57 */
58#define THMC50_REG_INTR 0x41
59
60static 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 };
63static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
64static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
65
66#define THMC50_REG_CONF_nFANOFF 0x20
67#define THMC50_REG_CONF_PROGRAMMED 0x08
68
69/* Each client has this additional data */
70struct thmc50_data {
71 struct device *hwmon_dev;
72
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];
83 s8 temp_critical[3];
84 u8 analog_out;
85 u8 alarms;
86};
87
88static int thmc50_detect(struct i2c_client *client,
89 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);
93static void thmc50_init_client(struct i2c_client *client);
94static struct thmc50_data *thmc50_update_device(struct device *dev);
95
96static const struct i2c_device_id thmc50_id[] = {
97 { "adm1022", adm1022 },
98 { "thmc50", thmc50 },
99 { }
100};
101MODULE_DEVICE_TABLE(i2c, thmc50_id);
102
103static struct i2c_driver thmc50_driver = {
104 .class = I2C_CLASS_HWMON,
105 .driver = {
106 .name = "thmc50",
107 },
108 .probe = thmc50_probe,
109 .remove = thmc50_remove,
110 .id_table = thmc50_id,
111 .detect = thmc50_detect,
112 .address_list = normal_i2c,
113};
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);
128 int config;
129 unsigned long tmp;
130 int err;
131
132 err = kstrtoul(buf, 10, &tmp);
133 if (err)
134 return err;
135
136 mutex_lock(&data->update_lock);
137 data->analog_out = clamp_val(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);
182 long val;
183 int err;
184
185 err = kstrtol(buf, 10, &val);
186 if (err)
187 return err;
188
189 mutex_lock(&data->update_lock);
190 data->temp_min[nr] = clamp_val(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);
211 long val;
212 int err;
213
214 err = kstrtol(buf, 10, &val);
215 if (err)
216 return err;
217
218 mutex_lock(&data->update_lock);
219 data->temp_max[nr] = clamp_val(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
226static 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
235static 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
244#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, \
250 show_temp_max, set_temp_max, offset - 1); \
251static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO, \
252 show_temp_critical, NULL, offset - 1);
253
254temp_reg(1);
255temp_reg(2);
256temp_reg(3);
257
258static 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
264static 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,
272 &sensor_dev_attr_temp1_crit.dev_attr.attr,
273 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
274 &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,
277 &sensor_dev_attr_temp2_crit.dev_attr.attr,
278 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
279 &sensor_dev_attr_temp2_fault.dev_attr.attr,
280 &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 */
290static struct attribute *temp3_attributes[] = {
291 &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,
294 &sensor_dev_attr_temp3_crit.dev_attr.attr,
295 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
296 &sensor_dev_attr_temp3_fault.dev_attr.attr,
297 NULL
298};
299
300static const struct attribute_group temp3_group = {
301 .attrs = temp3_attributes,
302};
303
304/* Return 0 if detection is successful, -ENODEV otherwise */
305static int thmc50_detect(struct i2c_client *client,
306 struct i2c_board_info *info)
307{
308 unsigned company;
309 unsigned revision;
310 unsigned config;
311 struct i2c_adapter *adapter = client->adapter;
312 const char *type_name;
313
314 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
315 pr_debug("thmc50: detect failed, smbus byte data not supported!\n");
316 return -ENODEV;
317 }
318
319 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
320 client->addr, i2c_adapter_id(client->adapter));
321
322 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
323 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
324 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
325 if (revision < 0xc0 || (config & 0x10))
326 return -ENODEV;
327
328 if (company == 0x41) {
329 int id = i2c_adapter_id(client->adapter);
330 int i;
331
332 type_name = "adm1022";
333 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
334 if (adm1022_temp3[i] == id &&
335 adm1022_temp3[i + 1] == client->addr) {
336 /* enable 2nd remote temp */
337 config |= (1 << 7);
338 i2c_smbus_write_byte_data(client,
339 THMC50_REG_CONF,
340 config);
341 break;
342 }
343 } else if (company == 0x49) {
344 type_name = "thmc50";
345 } else {
346 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
347 return -ENODEV;
348 }
349
350 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
351 type_name, (revision >> 4) - 0xc, revision & 0xf);
352
353 strlcpy(info->type, type_name, I2C_NAME_SIZE);
354
355 return 0;
356}
357
358static int thmc50_probe(struct i2c_client *client,
359 const struct i2c_device_id *id)
360{
361 struct thmc50_data *data;
362 int err;
363
364 data = devm_kzalloc(&client->dev, sizeof(struct thmc50_data),
365 GFP_KERNEL);
366 if (!data)
367 return -ENOMEM;
368
369 i2c_set_clientdata(client, data);
370 data->type = id->driver_data;
371 mutex_init(&data->update_lock);
372
373 thmc50_init_client(client);
374
375 /* Register sysfs hooks */
376 err = sysfs_create_group(&client->dev.kobj, &thmc50_group);
377 if (err)
378 return err;
379
380 /* Register ADM1022 sysfs hooks */
381 if (data->has_temp3) {
382 err = sysfs_create_group(&client->dev.kobj, &temp3_group);
383 if (err)
384 goto exit_remove_sysfs_thmc50;
385 }
386
387 /* Register a new directory entry with module sensors */
388 data->hwmon_dev = hwmon_device_register(&client->dev);
389 if (IS_ERR(data->hwmon_dev)) {
390 err = PTR_ERR(data->hwmon_dev);
391 goto exit_remove_sysfs;
392 }
393
394 return 0;
395
396exit_remove_sysfs:
397 if (data->has_temp3)
398 sysfs_remove_group(&client->dev.kobj, &temp3_group);
399exit_remove_sysfs_thmc50:
400 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
401 return err;
402}
403
404static int thmc50_remove(struct i2c_client *client)
405{
406 struct thmc50_data *data = i2c_get_clientdata(client);
407
408 hwmon_device_unregister(data->hwmon_dev);
409 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
410 if (data->has_temp3)
411 sysfs_remove_group(&client->dev.kobj, &temp3_group);
412
413 return 0;
414}
415
416static void thmc50_init_client(struct i2c_client *client)
417{
418 struct thmc50_data *data = i2c_get_clientdata(client);
419 int config;
420
421 data->analog_out = i2c_smbus_read_byte_data(client,
422 THMC50_REG_ANALOG_OUT);
423 /* set up to at least 1 */
424 if (data->analog_out == 0) {
425 data->analog_out = 1;
426 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
427 data->analog_out);
428 }
429 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
430 config |= 0x1; /* start the chip if it is in standby mode */
431 if (data->type == adm1022 && (config & (1 << 7)))
432 data->has_temp3 = 1;
433 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
434}
435
436static struct thmc50_data *thmc50_update_device(struct device *dev)
437{
438 struct i2c_client *client = to_i2c_client(dev);
439 struct thmc50_data *data = i2c_get_clientdata(client);
440 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
441
442 mutex_lock(&data->update_lock);
443
444 if (time_after(jiffies, data->last_updated + timeout)
445 || !data->valid) {
446
447 int temps = data->has_temp3 ? 3 : 2;
448 int i;
449 int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
450
451 prog &= THMC50_REG_CONF_PROGRAMMED;
452
453 for (i = 0; i < temps; i++) {
454 data->temp_input[i] = i2c_smbus_read_byte_data(client,
455 THMC50_REG_TEMP[i]);
456 data->temp_max[i] = i2c_smbus_read_byte_data(client,
457 THMC50_REG_TEMP_MAX[i]);
458 data->temp_min[i] = i2c_smbus_read_byte_data(client,
459 THMC50_REG_TEMP_MIN[i]);
460 data->temp_critical[i] =
461 i2c_smbus_read_byte_data(client,
462 prog ? THMC50_REG_TEMP_CRITICAL[i]
463 : THMC50_REG_TEMP_DEFAULT[i]);
464 }
465 data->analog_out =
466 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
467 data->alarms =
468 i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
469 data->last_updated = jiffies;
470 data->valid = 1;
471 }
472
473 mutex_unlock(&data->update_lock);
474
475 return data;
476}
477
478module_i2c_driver(thmc50_driver);
479
480MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
481MODULE_DESCRIPTION("THMC50 driver");