Loading...
1/*
2 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3 *
4 * Copyright (C) 2006 Corentin LABBE <corentin.labbe@geomatys.fr>
5 *
6 * Based on LM83 Driver by Jean Delvare <khali@linux-fr.org>
7 *
8 * Give only processor, motherboard temperatures and fan tachs
9 * Very rare chip please let me know if you use it
10 *
11 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
12 *
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation version 2 of the License
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/hwmon.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
37
38/*
39 * Addresses to scan
40 */
41
42static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END
44};
45
46/*
47 * The ADM1029 registers
48 * Manufacturer ID is 0x41 for Analog Devices
49 */
50
51#define ADM1029_REG_MAN_ID 0x0D
52#define ADM1029_REG_CHIP_ID 0x0E
53#define ADM1029_REG_CONFIG 0x01
54#define ADM1029_REG_NB_FAN_SUPPORT 0x02
55
56#define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
57
58#define ADM1029_REG_LOCAL_TEMP 0xA0
59#define ADM1029_REG_REMOTE1_TEMP 0xA1
60#define ADM1029_REG_REMOTE2_TEMP 0xA2
61
62#define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
63#define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
64#define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
65
66#define ADM1029_REG_LOCAL_TEMP_LOW 0x98
67#define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
68#define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
69
70#define ADM1029_REG_FAN1 0x70
71#define ADM1029_REG_FAN2 0x71
72
73#define ADM1029_REG_FAN1_MIN 0x78
74#define ADM1029_REG_FAN2_MIN 0x79
75
76#define ADM1029_REG_FAN1_CONFIG 0x68
77#define ADM1029_REG_FAN2_CONFIG 0x69
78
79#define TEMP_FROM_REG(val) ((val) * 1000)
80
81#define DIV_FROM_REG(val) ( 1 << (((val) >> 6) - 1))
82
83/* Registers to be checked by adm1029_update_device() */
84static const u8 ADM1029_REG_TEMP[] = {
85 ADM1029_REG_LOCAL_TEMP,
86 ADM1029_REG_REMOTE1_TEMP,
87 ADM1029_REG_REMOTE2_TEMP,
88 ADM1029_REG_LOCAL_TEMP_HIGH,
89 ADM1029_REG_REMOTE1_TEMP_HIGH,
90 ADM1029_REG_REMOTE2_TEMP_HIGH,
91 ADM1029_REG_LOCAL_TEMP_LOW,
92 ADM1029_REG_REMOTE1_TEMP_LOW,
93 ADM1029_REG_REMOTE2_TEMP_LOW,
94};
95
96static const u8 ADM1029_REG_FAN[] = {
97 ADM1029_REG_FAN1,
98 ADM1029_REG_FAN2,
99 ADM1029_REG_FAN1_MIN,
100 ADM1029_REG_FAN2_MIN,
101};
102
103static const u8 ADM1029_REG_FAN_DIV[] = {
104 ADM1029_REG_FAN1_CONFIG,
105 ADM1029_REG_FAN2_CONFIG,
106};
107
108/*
109 * Functions declaration
110 */
111
112static int adm1029_probe(struct i2c_client *client,
113 const struct i2c_device_id *id);
114static int adm1029_detect(struct i2c_client *client,
115 struct i2c_board_info *info);
116static int adm1029_remove(struct i2c_client *client);
117static struct adm1029_data *adm1029_update_device(struct device *dev);
118static int adm1029_init_client(struct i2c_client *client);
119
120/*
121 * Driver data (common to all clients)
122 */
123
124static const struct i2c_device_id adm1029_id[] = {
125 { "adm1029", 0 },
126 { }
127};
128MODULE_DEVICE_TABLE(i2c, adm1029_id);
129
130static struct i2c_driver adm1029_driver = {
131 .class = I2C_CLASS_HWMON,
132 .driver = {
133 .name = "adm1029",
134 },
135 .probe = adm1029_probe,
136 .remove = adm1029_remove,
137 .id_table = adm1029_id,
138 .detect = adm1029_detect,
139 .address_list = normal_i2c,
140};
141
142/*
143 * Client data (each client gets its own)
144 */
145
146struct adm1029_data {
147 struct device *hwmon_dev;
148 struct mutex update_lock;
149 char valid; /* zero until following fields are valid */
150 unsigned long last_updated; /* in jiffies */
151
152 /* registers values, signed for temperature, unsigned for other stuff */
153 s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
154 u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
155 u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
156};
157
158/*
159 * Sysfs stuff
160 */
161
162static ssize_t
163show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
164{
165 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166 struct adm1029_data *data = adm1029_update_device(dev);
167 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
168}
169
170static ssize_t
171show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
172{
173 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
174 struct adm1029_data *data = adm1029_update_device(dev);
175 u16 val;
176 if (data->fan[attr->index] == 0
177 || (data->fan_div[attr->index] & 0xC0) == 0
178 || data->fan[attr->index] == 255) {
179 return sprintf(buf, "0\n");
180 }
181
182 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
183 / data->fan[attr->index];
184 return sprintf(buf, "%d\n", val);
185}
186
187static ssize_t
188show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
189{
190 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
191 struct adm1029_data *data = adm1029_update_device(dev);
192 if ((data->fan_div[attr->index] & 0xC0) == 0)
193 return sprintf(buf, "0\n");
194 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
195}
196
197static ssize_t set_fan_div(struct device *dev,
198 struct device_attribute *devattr, const char *buf, size_t count)
199{
200 struct i2c_client *client = to_i2c_client(dev);
201 struct adm1029_data *data = i2c_get_clientdata(client);
202 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
203 long val = simple_strtol(buf, NULL, 10);
204 u8 reg;
205
206 mutex_lock(&data->update_lock);
207
208 /*Read actual config */
209 reg = i2c_smbus_read_byte_data(client,
210 ADM1029_REG_FAN_DIV[attr->index]);
211
212 switch (val) {
213 case 1:
214 val = 1;
215 break;
216 case 2:
217 val = 2;
218 break;
219 case 4:
220 val = 3;
221 break;
222 default:
223 mutex_unlock(&data->update_lock);
224 dev_err(&client->dev, "fan_div value %ld not "
225 "supported. Choose one of 1, 2 or 4!\n", val);
226 return -EINVAL;
227 }
228 /* Update the value */
229 reg = (reg & 0x3F) | (val << 6);
230
231 /* Write value */
232 i2c_smbus_write_byte_data(client,
233 ADM1029_REG_FAN_DIV[attr->index], reg);
234 mutex_unlock(&data->update_lock);
235
236 return count;
237}
238
239/*
240Access rights on sysfs, S_IRUGO stand for Is Readable by User, Group and Others
241 S_IWUSR stand for Is Writable by User
242*/
243static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
244static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
245static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
246
247static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
248static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
249static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
250
251static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
252static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
253static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
254
255static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
256static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
257
258static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
259static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
260
261static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
262 show_fan_div, set_fan_div, 0);
263static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
264 show_fan_div, set_fan_div, 1);
265
266static struct attribute *adm1029_attributes[] = {
267 &sensor_dev_attr_temp1_input.dev_attr.attr,
268 &sensor_dev_attr_temp1_min.dev_attr.attr,
269 &sensor_dev_attr_temp1_max.dev_attr.attr,
270 &sensor_dev_attr_temp2_input.dev_attr.attr,
271 &sensor_dev_attr_temp2_min.dev_attr.attr,
272 &sensor_dev_attr_temp2_max.dev_attr.attr,
273 &sensor_dev_attr_temp3_input.dev_attr.attr,
274 &sensor_dev_attr_temp3_min.dev_attr.attr,
275 &sensor_dev_attr_temp3_max.dev_attr.attr,
276 &sensor_dev_attr_fan1_input.dev_attr.attr,
277 &sensor_dev_attr_fan2_input.dev_attr.attr,
278 &sensor_dev_attr_fan1_min.dev_attr.attr,
279 &sensor_dev_attr_fan2_min.dev_attr.attr,
280 &sensor_dev_attr_fan1_div.dev_attr.attr,
281 &sensor_dev_attr_fan2_div.dev_attr.attr,
282 NULL
283};
284
285static const struct attribute_group adm1029_group = {
286 .attrs = adm1029_attributes,
287};
288
289/*
290 * Real code
291 */
292
293/* Return 0 if detection is successful, -ENODEV otherwise */
294static int adm1029_detect(struct i2c_client *client,
295 struct i2c_board_info *info)
296{
297 struct i2c_adapter *adapter = client->adapter;
298 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
299
300 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
301 return -ENODEV;
302
303 /* ADM1029 doesn't have CHIP ID, check just MAN ID
304 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
305 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
306 * documented
307 */
308
309 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
310 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
311 temp_devices_installed = i2c_smbus_read_byte_data(client,
312 ADM1029_REG_TEMP_DEVICES_INSTALLED);
313 nb_fan_support = i2c_smbus_read_byte_data(client,
314 ADM1029_REG_NB_FAN_SUPPORT);
315 /* 0x41 is Analog Devices */
316 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
317 || nb_fan_support != 0x03)
318 return -ENODEV;
319
320 if ((chip_id & 0xF0) != 0x00) {
321 /* There are no "official" CHIP ID, so actually
322 * we use Major/Minor revision for that */
323 pr_info("adm1029: Unknown major revision %x, "
324 "please let us know\n", chip_id);
325 return -ENODEV;
326 }
327
328 strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
329
330 return 0;
331}
332
333static int adm1029_probe(struct i2c_client *client,
334 const struct i2c_device_id *id)
335{
336 struct adm1029_data *data;
337 int err;
338
339 data = kzalloc(sizeof(struct adm1029_data), GFP_KERNEL);
340 if (!data) {
341 err = -ENOMEM;
342 goto exit;
343 }
344
345 i2c_set_clientdata(client, data);
346 mutex_init(&data->update_lock);
347
348 /*
349 * Initialize the ADM1029 chip
350 * Check config register
351 */
352 if (adm1029_init_client(client) == 0) {
353 err = -ENODEV;
354 goto exit_free;
355 }
356
357 /* Register sysfs hooks */
358 if ((err = sysfs_create_group(&client->dev.kobj, &adm1029_group)))
359 goto exit_free;
360
361 data->hwmon_dev = hwmon_device_register(&client->dev);
362 if (IS_ERR(data->hwmon_dev)) {
363 err = PTR_ERR(data->hwmon_dev);
364 goto exit_remove_files;
365 }
366
367 return 0;
368
369 exit_remove_files:
370 sysfs_remove_group(&client->dev.kobj, &adm1029_group);
371 exit_free:
372 kfree(data);
373 exit:
374 return err;
375}
376
377static int adm1029_init_client(struct i2c_client *client)
378{
379 u8 config;
380 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
381 if ((config & 0x10) == 0) {
382 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
383 config | 0x10);
384 }
385 /* recheck config */
386 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
387 if ((config & 0x10) == 0) {
388 dev_err(&client->dev, "Initialization failed!\n");
389 return 0;
390 }
391 return 1;
392}
393
394static int adm1029_remove(struct i2c_client *client)
395{
396 struct adm1029_data *data = i2c_get_clientdata(client);
397
398 hwmon_device_unregister(data->hwmon_dev);
399 sysfs_remove_group(&client->dev.kobj, &adm1029_group);
400
401 kfree(data);
402 return 0;
403}
404
405/*
406function that update the status of the chips (temperature for example)
407*/
408static struct adm1029_data *adm1029_update_device(struct device *dev)
409{
410 struct i2c_client *client = to_i2c_client(dev);
411 struct adm1029_data *data = i2c_get_clientdata(client);
412
413 mutex_lock(&data->update_lock);
414 /*
415 * Use the "cache" Luke, don't recheck values
416 * if there are already checked not a long time later
417 */
418 if (time_after(jiffies, data->last_updated + HZ * 2)
419 || !data->valid) {
420 int nr;
421
422 dev_dbg(&client->dev, "Updating adm1029 data\n");
423
424 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
425 data->temp[nr] =
426 i2c_smbus_read_byte_data(client,
427 ADM1029_REG_TEMP[nr]);
428 }
429 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
430 data->fan[nr] =
431 i2c_smbus_read_byte_data(client,
432 ADM1029_REG_FAN[nr]);
433 }
434 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
435 data->fan_div[nr] =
436 i2c_smbus_read_byte_data(client,
437 ADM1029_REG_FAN_DIV[nr]);
438 }
439
440 data->last_updated = jiffies;
441 data->valid = 1;
442 }
443
444 mutex_unlock(&data->update_lock);
445
446 return data;
447}
448
449/*
450 Common module stuff
451*/
452static int __init sensors_adm1029_init(void)
453{
454
455 return i2c_add_driver(&adm1029_driver);
456}
457
458static void __exit sensors_adm1029_exit(void)
459{
460
461 i2c_del_driver(&adm1029_driver);
462}
463
464MODULE_AUTHOR("Corentin LABBE <corentin.labbe@geomatys.fr>");
465MODULE_DESCRIPTION("adm1029 driver");
466MODULE_LICENSE("GPL v2");
467
468module_init(sensors_adm1029_init);
469module_exit(sensors_adm1029_exit);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
4 *
5 * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
6 *
7 * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
8 *
9 * Give only processor, motherboard temperatures and fan tachs
10 * Very rare chip please let me know if you use it
11 *
12 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/jiffies.h>
19#include <linux/i2c.h>
20#include <linux/hwmon-sysfs.h>
21#include <linux/hwmon.h>
22#include <linux/err.h>
23#include <linux/mutex.h>
24
25/*
26 * Addresses to scan
27 */
28
29static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
30 0x2e, 0x2f, I2C_CLIENT_END
31};
32
33/*
34 * The ADM1029 registers
35 * Manufacturer ID is 0x41 for Analog Devices
36 */
37
38#define ADM1029_REG_MAN_ID 0x0D
39#define ADM1029_REG_CHIP_ID 0x0E
40#define ADM1029_REG_CONFIG 0x01
41#define ADM1029_REG_NB_FAN_SUPPORT 0x02
42
43#define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
44
45#define ADM1029_REG_LOCAL_TEMP 0xA0
46#define ADM1029_REG_REMOTE1_TEMP 0xA1
47#define ADM1029_REG_REMOTE2_TEMP 0xA2
48
49#define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
50#define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
51#define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
52
53#define ADM1029_REG_LOCAL_TEMP_LOW 0x98
54#define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
55#define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
56
57#define ADM1029_REG_FAN1 0x70
58#define ADM1029_REG_FAN2 0x71
59
60#define ADM1029_REG_FAN1_MIN 0x78
61#define ADM1029_REG_FAN2_MIN 0x79
62
63#define ADM1029_REG_FAN1_CONFIG 0x68
64#define ADM1029_REG_FAN2_CONFIG 0x69
65
66#define TEMP_FROM_REG(val) ((val) * 1000)
67
68#define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
69
70/* Registers to be checked by adm1029_update_device() */
71static const u8 ADM1029_REG_TEMP[] = {
72 ADM1029_REG_LOCAL_TEMP,
73 ADM1029_REG_REMOTE1_TEMP,
74 ADM1029_REG_REMOTE2_TEMP,
75 ADM1029_REG_LOCAL_TEMP_HIGH,
76 ADM1029_REG_REMOTE1_TEMP_HIGH,
77 ADM1029_REG_REMOTE2_TEMP_HIGH,
78 ADM1029_REG_LOCAL_TEMP_LOW,
79 ADM1029_REG_REMOTE1_TEMP_LOW,
80 ADM1029_REG_REMOTE2_TEMP_LOW,
81};
82
83static const u8 ADM1029_REG_FAN[] = {
84 ADM1029_REG_FAN1,
85 ADM1029_REG_FAN2,
86 ADM1029_REG_FAN1_MIN,
87 ADM1029_REG_FAN2_MIN,
88};
89
90static const u8 ADM1029_REG_FAN_DIV[] = {
91 ADM1029_REG_FAN1_CONFIG,
92 ADM1029_REG_FAN2_CONFIG,
93};
94
95/*
96 * Client data (each client gets its own)
97 */
98
99struct adm1029_data {
100 struct i2c_client *client;
101 struct mutex update_lock; /* protect register access */
102 bool valid; /* false until following fields are valid */
103 unsigned long last_updated; /* in jiffies */
104
105 /* registers values, signed for temperature, unsigned for other stuff */
106 s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
107 u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
108 u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
109};
110
111/*
112 * function that update the status of the chips (temperature for example)
113 */
114static struct adm1029_data *adm1029_update_device(struct device *dev)
115{
116 struct adm1029_data *data = dev_get_drvdata(dev);
117 struct i2c_client *client = data->client;
118
119 mutex_lock(&data->update_lock);
120 /*
121 * Use the "cache" Luke, don't recheck values
122 * if there are already checked not a long time later
123 */
124 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
125 int nr;
126
127 dev_dbg(&client->dev, "Updating adm1029 data\n");
128
129 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
130 data->temp[nr] =
131 i2c_smbus_read_byte_data(client,
132 ADM1029_REG_TEMP[nr]);
133 }
134 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
135 data->fan[nr] =
136 i2c_smbus_read_byte_data(client,
137 ADM1029_REG_FAN[nr]);
138 }
139 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
140 data->fan_div[nr] =
141 i2c_smbus_read_byte_data(client,
142 ADM1029_REG_FAN_DIV[nr]);
143 }
144
145 data->last_updated = jiffies;
146 data->valid = true;
147 }
148
149 mutex_unlock(&data->update_lock);
150
151 return data;
152}
153
154/*
155 * Sysfs stuff
156 */
157
158static ssize_t
159temp_show(struct device *dev, struct device_attribute *devattr, char *buf)
160{
161 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
162 struct adm1029_data *data = adm1029_update_device(dev);
163
164 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
165}
166
167static ssize_t
168fan_show(struct device *dev, struct device_attribute *devattr, char *buf)
169{
170 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
171 struct adm1029_data *data = adm1029_update_device(dev);
172 u16 val;
173
174 if (data->fan[attr->index] == 0 ||
175 (data->fan_div[attr->index] & 0xC0) == 0 ||
176 data->fan[attr->index] == 255) {
177 return sprintf(buf, "0\n");
178 }
179
180 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
181 / data->fan[attr->index];
182 return sprintf(buf, "%d\n", val);
183}
184
185static ssize_t
186fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
187{
188 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
189 struct adm1029_data *data = adm1029_update_device(dev);
190
191 if ((data->fan_div[attr->index] & 0xC0) == 0)
192 return sprintf(buf, "0\n");
193 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
194}
195
196static ssize_t fan_div_store(struct device *dev,
197 struct device_attribute *devattr,
198 const char *buf, size_t count)
199{
200 struct adm1029_data *data = dev_get_drvdata(dev);
201 struct i2c_client *client = data->client;
202 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
203 u8 reg;
204 long val;
205 int ret = kstrtol(buf, 10, &val);
206
207 if (ret < 0)
208 return ret;
209
210 mutex_lock(&data->update_lock);
211
212 /*Read actual config */
213 reg = i2c_smbus_read_byte_data(client,
214 ADM1029_REG_FAN_DIV[attr->index]);
215
216 switch (val) {
217 case 1:
218 val = 1;
219 break;
220 case 2:
221 val = 2;
222 break;
223 case 4:
224 val = 3;
225 break;
226 default:
227 mutex_unlock(&data->update_lock);
228 dev_err(&client->dev,
229 "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
230 val);
231 return -EINVAL;
232 }
233 /* Update the value */
234 reg = (reg & 0x3F) | (val << 6);
235
236 /* Update the cache */
237 data->fan_div[attr->index] = reg;
238
239 /* Write value */
240 i2c_smbus_write_byte_data(client,
241 ADM1029_REG_FAN_DIV[attr->index], reg);
242 mutex_unlock(&data->update_lock);
243
244 return count;
245}
246
247/* Access rights on sysfs. */
248static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
249static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
250static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
251
252static SENSOR_DEVICE_ATTR_RO(temp1_max, temp, 3);
253static SENSOR_DEVICE_ATTR_RO(temp2_max, temp, 4);
254static SENSOR_DEVICE_ATTR_RO(temp3_max, temp, 5);
255
256static SENSOR_DEVICE_ATTR_RO(temp1_min, temp, 6);
257static SENSOR_DEVICE_ATTR_RO(temp2_min, temp, 7);
258static SENSOR_DEVICE_ATTR_RO(temp3_min, temp, 8);
259
260static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
261static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
262
263static SENSOR_DEVICE_ATTR_RO(fan1_min, fan, 2);
264static SENSOR_DEVICE_ATTR_RO(fan2_min, fan, 3);
265
266static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
267static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
268
269static struct attribute *adm1029_attrs[] = {
270 &sensor_dev_attr_temp1_input.dev_attr.attr,
271 &sensor_dev_attr_temp1_min.dev_attr.attr,
272 &sensor_dev_attr_temp1_max.dev_attr.attr,
273 &sensor_dev_attr_temp2_input.dev_attr.attr,
274 &sensor_dev_attr_temp2_min.dev_attr.attr,
275 &sensor_dev_attr_temp2_max.dev_attr.attr,
276 &sensor_dev_attr_temp3_input.dev_attr.attr,
277 &sensor_dev_attr_temp3_min.dev_attr.attr,
278 &sensor_dev_attr_temp3_max.dev_attr.attr,
279 &sensor_dev_attr_fan1_input.dev_attr.attr,
280 &sensor_dev_attr_fan2_input.dev_attr.attr,
281 &sensor_dev_attr_fan1_min.dev_attr.attr,
282 &sensor_dev_attr_fan2_min.dev_attr.attr,
283 &sensor_dev_attr_fan1_div.dev_attr.attr,
284 &sensor_dev_attr_fan2_div.dev_attr.attr,
285 NULL
286};
287
288ATTRIBUTE_GROUPS(adm1029);
289
290/*
291 * Real code
292 */
293
294/* Return 0 if detection is successful, -ENODEV otherwise */
295static int adm1029_detect(struct i2c_client *client,
296 struct i2c_board_info *info)
297{
298 struct i2c_adapter *adapter = client->adapter;
299 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
300
301 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
302 return -ENODEV;
303
304 /*
305 * ADM1029 doesn't have CHIP ID, check just MAN ID
306 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
307 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
308 * documented
309 */
310
311 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
312 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
313 temp_devices_installed = i2c_smbus_read_byte_data(client,
314 ADM1029_REG_TEMP_DEVICES_INSTALLED);
315 nb_fan_support = i2c_smbus_read_byte_data(client,
316 ADM1029_REG_NB_FAN_SUPPORT);
317 /* 0x41 is Analog Devices */
318 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01 ||
319 nb_fan_support != 0x03)
320 return -ENODEV;
321
322 if ((chip_id & 0xF0) != 0x00) {
323 /*
324 * There are no "official" CHIP ID, so actually
325 * we use Major/Minor revision for that
326 */
327 pr_info("Unknown major revision %x, please let us know\n",
328 chip_id);
329 return -ENODEV;
330 }
331
332 strscpy(info->type, "adm1029", I2C_NAME_SIZE);
333
334 return 0;
335}
336
337static int adm1029_init_client(struct i2c_client *client)
338{
339 u8 config;
340
341 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
342 if ((config & 0x10) == 0) {
343 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
344 config | 0x10);
345 }
346 /* recheck config */
347 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
348 if ((config & 0x10) == 0) {
349 dev_err(&client->dev, "Initialization failed!\n");
350 return 0;
351 }
352 return 1;
353}
354
355static int adm1029_probe(struct i2c_client *client)
356{
357 struct device *dev = &client->dev;
358 struct adm1029_data *data;
359 struct device *hwmon_dev;
360
361 data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
362 if (!data)
363 return -ENOMEM;
364
365 data->client = client;
366 mutex_init(&data->update_lock);
367
368 /*
369 * Initialize the ADM1029 chip
370 * Check config register
371 */
372 if (adm1029_init_client(client) == 0)
373 return -ENODEV;
374
375 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
376 data,
377 adm1029_groups);
378 return PTR_ERR_OR_ZERO(hwmon_dev);
379}
380
381static const struct i2c_device_id adm1029_id[] = {
382 { "adm1029", 0 },
383 { }
384};
385MODULE_DEVICE_TABLE(i2c, adm1029_id);
386
387static struct i2c_driver adm1029_driver = {
388 .class = I2C_CLASS_HWMON,
389 .driver = {
390 .name = "adm1029",
391 },
392 .probe = adm1029_probe,
393 .id_table = adm1029_id,
394 .detect = adm1029_detect,
395 .address_list = normal_i2c,
396};
397
398module_i2c_driver(adm1029_driver);
399
400MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
401MODULE_DESCRIPTION("adm1029 driver");
402MODULE_LICENSE("GPL v2");