1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/config.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/i2c.h>
31#include <linux/i2c-sensor.h>
32#include <linux/i2c-vid.h>
33
34
35static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
36static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
37
38
39SENSORS_INSMOD_1(adm1026);
40
41static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1 };
43static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1 };
45static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1 };
47static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1 };
49static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
50module_param_array(gpio_input,int,NULL,0);
51MODULE_PARM_DESC(gpio_input,"List of GPIO pins (0-16) to program as inputs");
52module_param_array(gpio_output,int,NULL,0);
53MODULE_PARM_DESC(gpio_output,"List of GPIO pins (0-16) to program as "
54 "outputs");
55module_param_array(gpio_inverted,int,NULL,0);
56MODULE_PARM_DESC(gpio_inverted,"List of GPIO pins (0-16) to program as "
57 "inverted");
58module_param_array(gpio_normal,int,NULL,0);
59MODULE_PARM_DESC(gpio_normal,"List of GPIO pins (0-16) to program as "
60 "normal/non-inverted");
61module_param_array(gpio_fan,int,NULL,0);
62MODULE_PARM_DESC(gpio_fan,"List of GPIO pins (0-7) to program as fan tachs");
63
64
65
66
67#define ADM1026_REG_CONFIG1 0x00
68#define CFG1_MONITOR 0x01
69#define CFG1_INT_ENABLE 0x02
70#define CFG1_INT_CLEAR 0x04
71#define CFG1_AIN8_9 0x08
72#define CFG1_THERM_HOT 0x10
73#define CFG1_DAC_AFC 0x20
74#define CFG1_PWM_AFC 0x40
75#define CFG1_RESET 0x80
76#define ADM1026_REG_CONFIG2 0x01
77
78#define ADM1026_REG_CONFIG3 0x07
79#define CFG3_GPIO16_ENABLE 0x01
80#define CFG3_CI_CLEAR 0x02
81#define CFG3_VREF_250 0x04
82#define CFG3_GPIO16_DIR 0x40
83#define CFG3_GPIO16_POL 0x80
84#define ADM1026_REG_E2CONFIG 0x13
85#define E2CFG_READ 0x01
86#define E2CFG_WRITE 0x02
87#define E2CFG_ERASE 0x04
88#define E2CFG_ROM 0x08
89#define E2CFG_CLK_EXT 0x80
90
91
92
93
94
95
96
97
98
99
100
101
102static u16 ADM1026_REG_IN[] = {
103 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
104 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
105 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
106 };
107static u16 ADM1026_REG_IN_MIN[] = {
108 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
109 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
110 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
111 };
112static u16 ADM1026_REG_IN_MAX[] = {
113 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
114 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
115 0x43, 0x44, 0x45, 0x46, 0x47
116 };
117
118
119
120
121
122
123static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
124static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
125static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
126static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
127static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
128static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
129
130#define ADM1026_REG_FAN(nr) (0x38 + (nr))
131#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
132#define ADM1026_REG_FAN_DIV_0_3 0x02
133#define ADM1026_REG_FAN_DIV_4_7 0x03
134
135#define ADM1026_REG_DAC 0x04
136#define ADM1026_REG_PWM 0x05
137
138#define ADM1026_REG_GPIO_CFG_0_3 0x08
139#define ADM1026_REG_GPIO_CFG_4_7 0x09
140#define ADM1026_REG_GPIO_CFG_8_11 0x0a
141#define ADM1026_REG_GPIO_CFG_12_15 0x0b
142
143#define ADM1026_REG_GPIO_STATUS_0_7 0x24
144#define ADM1026_REG_GPIO_STATUS_8_15 0x25
145
146#define ADM1026_REG_GPIO_MASK_0_7 0x1c
147#define ADM1026_REG_GPIO_MASK_8_15 0x1d
148
149
150#define ADM1026_REG_COMPANY 0x16
151#define ADM1026_REG_VERSTEP 0x17
152
153#define ADM1026_COMPANY_ANALOG_DEV 0x41
154#define ADM1026_VERSTEP_GENERIC 0x40
155#define ADM1026_VERSTEP_ADM1026 0x44
156
157#define ADM1026_REG_MASK1 0x18
158#define ADM1026_REG_MASK2 0x19
159#define ADM1026_REG_MASK3 0x1a
160#define ADM1026_REG_MASK4 0x1b
161
162#define ADM1026_REG_STATUS1 0x20
163#define ADM1026_REG_STATUS2 0x21
164#define ADM1026_REG_STATUS3 0x22
165#define ADM1026_REG_STATUS4 0x23
166
167#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
168#define ADM1026_FAN_CONTROL_TEMP_RANGE 20
169#define ADM1026_PWM_MAX 255
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187static int adm1026_scaling[] = {
188 2250, 2250, 2250, 2250, 2250, 2250,
189 1875, 1875, 1875, 1875, 3000, 3330,
190 3330, 4995, 2250, 12000, 13875
191 };
192#define NEG12_OFFSET 16000
193#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
194#define INS_TO_REG(n,val) (SENSORS_LIMIT(SCALE(val,adm1026_scaling[n],192),\
195 0,255))
196#define INS_FROM_REG(n,val) (SCALE(val,192,adm1026_scaling[n]))
197
198
199
200
201
202#define FAN_TO_REG(val,div) ((val)<=0 ? 0xff : SENSORS_LIMIT(1350000/((val)*\
203 (div)),1,254))
204#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==0xff ? 0 : 1350000/((val)*\
205 (div)))
206#define DIV_FROM_REG(val) (1<<(val))
207#define DIV_TO_REG(val) ((val)>=8 ? 3 : (val)>=4 ? 2 : (val)>=2 ? 1 : 0)
208
209
210#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
211 -127,127))
212#define TEMP_FROM_REG(val) ((val) * 1000)
213#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
214 -127,127))
215#define OFFSET_FROM_REG(val) ((val) * 1000)
216
217#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
218#define PWM_FROM_REG(val) (val)
219
220#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
221#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
222
223
224
225
226
227#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500),0,255))
228#define DAC_FROM_REG(val) (((val)*2500)/255)
229
230
231#define ADM1026_INIT_VRM 91
232
233
234
235
236
237
238
239
240
241
242
243
244#define ADM1026_DATA_INTERVAL (1 * HZ)
245#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
246
247
248
249
250
251
252
253struct pwm_data {
254 u8 pwm;
255 u8 enable;
256 u8 auto_pwm_min;
257};
258
259struct adm1026_data {
260 struct i2c_client client;
261 struct semaphore lock;
262 enum chips type;
263
264 struct semaphore update_lock;
265 int valid;
266 unsigned long last_reading;
267 unsigned long last_config;
268
269 u8 in[17];
270 u8 in_max[17];
271 u8 in_min[17];
272 s8 temp[3];
273 s8 temp_min[3];
274 s8 temp_max[3];
275 s8 temp_tmin[3];
276 s8 temp_crit[3];
277 s8 temp_offset[3];
278 u8 fan[8];
279 u8 fan_min[8];
280 u8 fan_div[8];
281 struct pwm_data pwm1;
282 int vid;
283 u8 vrm;
284 u8 analog_out;
285 long alarms;
286 long alarm_mask;
287 long gpio;
288 long gpio_mask;
289 u8 gpio_config[17];
290 u8 config1;
291 u8 config2;
292 u8 config3;
293};
294
295static int adm1026_attach_adapter(struct i2c_adapter *adapter);
296static int adm1026_detect(struct i2c_adapter *adapter, int address,
297 int kind);
298static int adm1026_detach_client(struct i2c_client *client);
299static int adm1026_read_value(struct i2c_client *client, u8 register);
300static int adm1026_write_value(struct i2c_client *client, u8 register,
301 int value);
302static void adm1026_print_gpio(struct i2c_client *client);
303static void adm1026_fixup_gpio(struct i2c_client *client);
304static struct adm1026_data *adm1026_update_device(struct device *dev);
305static void adm1026_init_client(struct i2c_client *client);
306
307
308static struct i2c_driver adm1026_driver = {
309 .owner = THIS_MODULE,
310 .name = "adm1026",
311 .flags = I2C_DF_NOTIFY,
312 .attach_adapter = adm1026_attach_adapter,
313 .detach_client = adm1026_detach_client,
314};
315
316static int adm1026_id;
317
318int adm1026_attach_adapter(struct i2c_adapter *adapter)
319{
320 if (!(adapter->class & I2C_CLASS_HWMON)) {
321 return 0;
322 }
323 return i2c_detect(adapter, &addr_data, adm1026_detect);
324}
325
326int adm1026_detach_client(struct i2c_client *client)
327{
328 i2c_detach_client(client);
329 kfree(client);
330 return 0;
331}
332
333int adm1026_read_value(struct i2c_client *client, u8 reg)
334{
335 int res;
336
337 if (reg < 0x80) {
338
339 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
340 } else {
341
342 res = 0;
343 }
344 return res;
345}
346
347int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
348{
349 int res;
350
351 if (reg < 0x80) {
352
353 res = i2c_smbus_write_byte_data(client, reg, value);
354 } else {
355
356 res = 0;
357 }
358 return res;
359}
360
361void adm1026_init_client(struct i2c_client *client)
362{
363 int value, i;
364 struct adm1026_data *data = i2c_get_clientdata(client);
365
366 dev_dbg(&client->dev,"(%d): Initializing device\n", client->id);
367
368 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
369 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
370 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
371
372
373 dev_dbg(&client->dev, "(%d): ADM1026_REG_CONFIG1 is: 0x%02x\n",
374 client->id, data->config1);
375 if ((data->config1 & CFG1_MONITOR) == 0) {
376 dev_dbg(&client->dev, "(%d): Monitoring not currently "
377 "enabled.\n", client->id);
378 }
379 if (data->config1 & CFG1_INT_ENABLE) {
380 dev_dbg(&client->dev, "(%d): SMBALERT interrupts are "
381 "enabled.\n", client->id);
382 }
383 if (data->config1 & CFG1_AIN8_9) {
384 dev_dbg(&client->dev, "(%d): in8 and in9 enabled. "
385 "temp3 disabled.\n", client->id);
386 } else {
387 dev_dbg(&client->dev, "(%d): temp3 enabled. in8 and "
388 "in9 disabled.\n", client->id);
389 }
390 if (data->config1 & CFG1_THERM_HOT) {
391 dev_dbg(&client->dev, "(%d): Automatic THERM, PWM, "
392 "and temp limits enabled.\n", client->id);
393 }
394
395 value = data->config3;
396 if (data->config3 & CFG3_GPIO16_ENABLE) {
397 dev_dbg(&client->dev, "(%d): GPIO16 enabled. THERM"
398 "pin disabled.\n", client->id);
399 } else {
400 dev_dbg(&client->dev, "(%d): THERM pin enabled. "
401 "GPIO16 disabled.\n", client->id);
402 }
403 if (data->config3 & CFG3_VREF_250) {
404 dev_dbg(&client->dev, "(%d): Vref is 2.50 Volts.\n",
405 client->id);
406 } else {
407 dev_dbg(&client->dev, "(%d): Vref is 1.82 Volts.\n",
408 client->id);
409 }
410
411 value = 0;
412 for (i = 0;i <= 15;++i) {
413 if ((i & 0x03) == 0) {
414 value = adm1026_read_value(client,
415 ADM1026_REG_GPIO_CFG_0_3 + i/4);
416 }
417 data->gpio_config[i] = value & 0x03;
418 value >>= 2;
419 }
420 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
421
422
423 adm1026_print_gpio(client);
424
425
426
427
428 if (client->id == 0
429 && (gpio_input[0] != -1 || gpio_output[0] != -1
430 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
431 || gpio_fan[0] != -1)) {
432 adm1026_fixup_gpio(client);
433 }
434
435
436
437
438
439
440
441
442
443
444
445
446 data->pwm1.auto_pwm_min=255;
447
448 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
449
450 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
451 dev_dbg(&client->dev, "(%d): Setting CONFIG to: 0x%02x\n",
452 client->id, value);
453 data->config1 = value;
454 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
455
456
457 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
458 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
459 for (i = 0;i <= 7;++i) {
460 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
461 value >>= 2;
462 }
463}
464
465void adm1026_print_gpio(struct i2c_client *client)
466{
467 struct adm1026_data *data = i2c_get_clientdata(client);
468 int i;
469
470 dev_dbg(&client->dev, "(%d): GPIO config is:", client->id);
471 for (i = 0;i <= 7;++i) {
472 if (data->config2 & (1 << i)) {
473 dev_dbg(&client->dev, "\t(%d): %sGP%s%d\n", client->id,
474 data->gpio_config[i] & 0x02 ? "" : "!",
475 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
476 i);
477 } else {
478 dev_dbg(&client->dev, "\t(%d): FAN%d\n",
479 client->id, i);
480 }
481 }
482 for (i = 8;i <= 15;++i) {
483 dev_dbg(&client->dev, "\t(%d): %sGP%s%d\n", client->id,
484 data->gpio_config[i] & 0x02 ? "" : "!",
485 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
486 i);
487 }
488 if (data->config3 & CFG3_GPIO16_ENABLE) {
489 dev_dbg(&client->dev, "\t(%d): %sGP%s16\n", client->id,
490 data->gpio_config[16] & 0x02 ? "" : "!",
491 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
492 } else {
493
494 dev_dbg(&client->dev, "\t(%d): THERM\n", client->id);
495 }
496}
497
498void adm1026_fixup_gpio(struct i2c_client *client)
499{
500 struct adm1026_data *data = i2c_get_clientdata(client);
501 int i;
502 int value;
503
504
505
506
507
508
509
510
511 for (i = 0;i <= 16;++i) {
512 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
513 data->gpio_config[gpio_output[i]] |= 0x01;
514 }
515
516 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
517 data->config2 |= 1 << gpio_output[i];
518 }
519 }
520
521
522 for (i = 0;i <= 16;++i) {
523 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
524 data->gpio_config[gpio_input[i]] &= ~ 0x01;
525 }
526
527 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
528 data->config2 |= 1 << gpio_input[i];
529 }
530 }
531
532
533 for (i = 0;i <= 16;++i) {
534 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
535 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
536 }
537 }
538
539
540 for (i = 0;i <= 16;++i) {
541 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
542 data->gpio_config[gpio_normal[i]] |= 0x02;
543 }
544 }
545
546
547 for (i = 0;i <= 7;++i) {
548 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
549 data->config2 &= ~(1 << gpio_fan[i]);
550 }
551 }
552
553
554 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
555 data->config3 = (data->config3 & 0x3f)
556 | ((data->gpio_config[16] & 0x03) << 6);
557 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
558 for (i = 15, value = 0;i >= 0;--i) {
559 value <<= 2;
560 value |= data->gpio_config[i] & 0x03;
561 if ((i & 0x03) == 0) {
562 adm1026_write_value(client,
563 ADM1026_REG_GPIO_CFG_0_3 + i/4,
564 value);
565 value = 0;
566 }
567 }
568
569
570 adm1026_print_gpio(client);
571}
572
573
574static struct adm1026_data *adm1026_update_device(struct device *dev)
575{
576 struct i2c_client *client = to_i2c_client(dev);
577 struct adm1026_data *data = i2c_get_clientdata(client);
578 int i;
579 long value, alarms, gpio;
580
581 down(&data->update_lock);
582 if (!data->valid
583 || (jiffies - data->last_reading > ADM1026_DATA_INTERVAL)) {
584
585 dev_dbg(&client->dev,"(%d): Reading sensor values\n",
586 client->id);
587 for (i = 0;i <= 16;++i) {
588 data->in[i] =
589 adm1026_read_value(client, ADM1026_REG_IN[i]);
590 }
591
592 for (i = 0;i <= 7;++i) {
593 data->fan[i] =
594 adm1026_read_value(client, ADM1026_REG_FAN(i));
595 }
596
597 for (i = 0;i <= 2;++i) {
598
599
600 data->temp[i] =
601 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
602 }
603
604 data->pwm1.pwm = adm1026_read_value(client,
605 ADM1026_REG_PWM);
606 data->analog_out = adm1026_read_value(client,
607 ADM1026_REG_DAC);
608
609 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
610 gpio = alarms & 0x80 ? 0x0100 : 0;
611 alarms &= 0x7f;
612 alarms <<= 8;
613 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
614 alarms <<= 8;
615 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
616 alarms <<= 8;
617 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
618 data->alarms = alarms;
619
620
621 gpio |= adm1026_read_value(client,
622 ADM1026_REG_GPIO_STATUS_8_15);
623 gpio <<= 8;
624 gpio |= adm1026_read_value(client,
625 ADM1026_REG_GPIO_STATUS_0_7);
626 data->gpio = gpio;
627
628 data->last_reading = jiffies;
629 };
630
631 if (!data->valid || (jiffies - data->last_config >
632 ADM1026_CONFIG_INTERVAL)) {
633
634 dev_dbg(&client->dev, "(%d): Reading config values\n",
635 client->id);
636 for (i = 0;i <= 16;++i) {
637 data->in_min[i] = adm1026_read_value(client,
638 ADM1026_REG_IN_MIN[i]);
639 data->in_max[i] = adm1026_read_value(client,
640 ADM1026_REG_IN_MAX[i]);
641 }
642
643 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
644 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
645 << 8);
646 for (i = 0;i <= 7;++i) {
647 data->fan_min[i] = adm1026_read_value(client,
648 ADM1026_REG_FAN_MIN(i));
649 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
650 value >>= 2;
651 }
652
653 for (i = 0; i <= 2; ++i) {
654
655
656
657 data->temp_min[i] = adm1026_read_value(client,
658 ADM1026_REG_TEMP_MIN[i]);
659 data->temp_max[i] = adm1026_read_value(client,
660 ADM1026_REG_TEMP_MAX[i]);
661 data->temp_tmin[i] = adm1026_read_value(client,
662 ADM1026_REG_TEMP_TMIN[i]);
663 data->temp_crit[i] = adm1026_read_value(client,
664 ADM1026_REG_TEMP_THERM[i]);
665 data->temp_offset[i] = adm1026_read_value(client,
666 ADM1026_REG_TEMP_OFFSET[i]);
667 }
668
669
670 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
671 gpio = alarms & 0x80 ? 0x0100 : 0;
672 alarms = (alarms & 0x7f) << 8;
673 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
674 alarms <<= 8;
675 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
676 alarms <<= 8;
677 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
678 data->alarm_mask = alarms;
679
680
681 gpio |= adm1026_read_value(client,
682 ADM1026_REG_GPIO_MASK_8_15);
683 gpio <<= 8;
684 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
685 data->gpio_mask = gpio;
686
687
688 data->config1 = adm1026_read_value(client,
689 ADM1026_REG_CONFIG1);
690 if (data->config1 & CFG1_PWM_AFC) {
691 data->pwm1.enable = 2;
692 data->pwm1.auto_pwm_min =
693 PWM_MIN_FROM_REG(data->pwm1.pwm);
694 }
695
696 data->config2 = adm1026_read_value(client,
697 ADM1026_REG_CONFIG2);
698 data->config3 = adm1026_read_value(client,
699 ADM1026_REG_CONFIG3);
700 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
701
702 value = 0;
703 for (i = 0;i <= 15;++i) {
704 if ((i & 0x03) == 0) {
705 value = adm1026_read_value(client,
706 ADM1026_REG_GPIO_CFG_0_3 + i/4);
707 }
708 data->gpio_config[i] = value & 0x03;
709 value >>= 2;
710 }
711
712 data->last_config = jiffies;
713 };
714
715 dev_dbg(&client->dev, "(%d): Setting VID from GPIO11-15.\n",
716 client->id);
717 data->vid = (data->gpio >> 11) & 0x1f;
718 data->valid = 1;
719 up(&data->update_lock);
720 return data;
721}
722
723static ssize_t show_in(struct device *dev, char *buf, int nr)
724{
725 struct adm1026_data *data = adm1026_update_device(dev);
726 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]));
727}
728static ssize_t show_in_min(struct device *dev, char *buf, int nr)
729{
730 struct adm1026_data *data = adm1026_update_device(dev);
731 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]));
732}
733static ssize_t set_in_min(struct device *dev, const char *buf,
734 size_t count, int nr)
735{
736 struct i2c_client *client = to_i2c_client(dev);
737 struct adm1026_data *data = i2c_get_clientdata(client);
738 int val;
739
740 down(&data->update_lock);
741 val = simple_strtol(buf, NULL, 10);
742 data->in_min[nr] = INS_TO_REG(nr, val);
743 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
744 up(&data->update_lock);
745 return count;
746}
747static ssize_t show_in_max(struct device *dev, char *buf, int nr)
748{
749 struct adm1026_data *data = adm1026_update_device(dev);
750 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]));
751}
752static ssize_t set_in_max(struct device *dev, const char *buf,
753 size_t count, int nr)
754{
755 struct i2c_client *client = to_i2c_client(dev);
756 struct adm1026_data *data = i2c_get_clientdata(client);
757 int val;
758
759 down(&data->update_lock);
760 val = simple_strtol(buf, NULL, 10);
761 data->in_max[nr] = INS_TO_REG(nr, val);
762 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
763 up(&data->update_lock);
764 return count;
765}
766
767#define in_reg(offset) \
768static ssize_t show_in##offset (struct device *dev, char *buf) \
769{ \
770 return show_in(dev, buf, offset); \
771} \
772static ssize_t show_in##offset##_min (struct device *dev, char *buf) \
773{ \
774 return show_in_min(dev, buf, offset); \
775} \
776static ssize_t set_in##offset##_min (struct device *dev, \
777 const char *buf, size_t count) \
778{ \
779 return set_in_min(dev, buf, count, offset); \
780} \
781static ssize_t show_in##offset##_max (struct device *dev, char *buf) \
782{ \
783 return show_in_max(dev, buf, offset); \
784} \
785static ssize_t set_in##offset##_max (struct device *dev, \
786 const char *buf, size_t count) \
787{ \
788 return set_in_max(dev, buf, count, offset); \
789} \
790static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
791static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
792 show_in##offset##_min, set_in##offset##_min); \
793static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
794 show_in##offset##_max, set_in##offset##_max);
795
796
797in_reg(0);
798in_reg(1);
799in_reg(2);
800in_reg(3);
801in_reg(4);
802in_reg(5);
803in_reg(6);
804in_reg(7);
805in_reg(8);
806in_reg(9);
807in_reg(10);
808in_reg(11);
809in_reg(12);
810in_reg(13);
811in_reg(14);
812in_reg(15);
813
814static ssize_t show_in16(struct device *dev, char *buf)
815{
816 struct adm1026_data *data = adm1026_update_device(dev);
817 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in[16]) -
818 NEG12_OFFSET);
819}
820static ssize_t show_in16_min(struct device *dev, char *buf)
821{
822 struct adm1026_data *data = adm1026_update_device(dev);
823 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_min[16])
824 - NEG12_OFFSET);
825}
826static ssize_t set_in16_min(struct device *dev, const char *buf, size_t count)
827{
828 struct i2c_client *client = to_i2c_client(dev);
829 struct adm1026_data *data = i2c_get_clientdata(client);
830 int val;
831
832 down(&data->update_lock);
833 val = simple_strtol(buf, NULL, 10);
834 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
835 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
836 up(&data->update_lock);
837 return count;
838}
839static ssize_t show_in16_max(struct device *dev, char *buf)
840{
841 struct adm1026_data *data = adm1026_update_device(dev);
842 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_max[16])
843 - NEG12_OFFSET);
844}
845static ssize_t set_in16_max(struct device *dev, const char *buf, size_t count)
846{
847 struct i2c_client *client = to_i2c_client(dev);
848 struct adm1026_data *data = i2c_get_clientdata(client);
849 int val;
850
851 down(&data->update_lock);
852 val = simple_strtol(buf, NULL, 10);
853 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
854 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
855 up(&data->update_lock);
856 return count;
857}
858
859static DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL);
860static DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min);
861static DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max);
862
863
864
865
866
867
868static ssize_t show_fan(struct device *dev, char *buf, int nr)
869{
870 struct adm1026_data *data = adm1026_update_device(dev);
871 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
872 data->fan_div[nr]));
873}
874static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
875{
876 struct adm1026_data *data = adm1026_update_device(dev);
877 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
878 data->fan_div[nr]));
879}
880static ssize_t set_fan_min(struct device *dev, const char *buf,
881 size_t count, int nr)
882{
883 struct i2c_client *client = to_i2c_client(dev);
884 struct adm1026_data *data = i2c_get_clientdata(client);
885 int val;
886
887 down(&data->update_lock);
888 val = simple_strtol(buf, NULL, 10);
889 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
890 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
891 data->fan_min[nr]);
892 up(&data->update_lock);
893 return count;
894}
895
896#define fan_offset(offset) \
897static ssize_t show_fan_##offset (struct device *dev, char *buf) \
898{ \
899 return show_fan(dev, buf, offset - 1); \
900} \
901static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
902{ \
903 return show_fan_min(dev, buf, offset - 1); \
904} \
905static ssize_t set_fan_##offset##_min (struct device *dev, \
906 const char *buf, size_t count) \
907{ \
908 return set_fan_min(dev, buf, count, offset - 1); \
909} \
910static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
911static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
912 show_fan_##offset##_min, set_fan_##offset##_min);
913
914fan_offset(1);
915fan_offset(2);
916fan_offset(3);
917fan_offset(4);
918fan_offset(5);
919fan_offset(6);
920fan_offset(7);
921fan_offset(8);
922
923
924static void fixup_fan_min(struct device *dev, int fan, int old_div)
925{
926 struct i2c_client *client = to_i2c_client(dev);
927 struct adm1026_data *data = i2c_get_clientdata(client);
928 int new_min;
929 int new_div = data->fan_div[fan];
930
931
932 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
933 return;
934 }
935
936 new_min = data->fan_min[fan] * old_div / new_div;
937 new_min = SENSORS_LIMIT(new_min, 1, 254);
938 data->fan_min[fan] = new_min;
939 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
940}
941
942
943static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
944{
945 struct adm1026_data *data = adm1026_update_device(dev);
946 return sprintf(buf,"%d\n", data->fan_div[nr]);
947}
948static ssize_t set_fan_div(struct device *dev, const char *buf,
949 size_t count, int nr)
950{
951 struct i2c_client *client = to_i2c_client(dev);
952 struct adm1026_data *data = i2c_get_clientdata(client);
953 int val,orig_div,new_div,shift;
954
955 val = simple_strtol(buf, NULL, 10);
956 new_div = DIV_TO_REG(val);
957 if (new_div == 0) {
958 return -EINVAL;
959 }
960 down(&data->update_lock);
961 orig_div = data->fan_div[nr];
962 data->fan_div[nr] = DIV_FROM_REG(new_div);
963
964 if (nr < 4) {
965 shift = 2 * nr;
966 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
967 ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
968 (new_div << shift)));
969 } else {
970 shift = 2 * (nr - 4);
971 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
972 ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
973 (new_div << shift)));
974 }
975
976 if (data->fan_div[nr] != orig_div) {
977 fixup_fan_min(dev,nr,orig_div);
978 }
979 up(&data->update_lock);
980 return count;
981}
982
983#define fan_offset_div(offset) \
984static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
985{ \
986 return show_fan_div(dev, buf, offset - 1); \
987} \
988static ssize_t set_fan_##offset##_div (struct device *dev, \
989 const char *buf, size_t count) \
990{ \
991 return set_fan_div(dev, buf, count, offset - 1); \
992} \
993static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
994 show_fan_##offset##_div, set_fan_##offset##_div);
995
996fan_offset_div(1);
997fan_offset_div(2);
998fan_offset_div(3);
999fan_offset_div(4);
1000fan_offset_div(5);
1001fan_offset_div(6);
1002fan_offset_div(7);
1003fan_offset_div(8);
1004
1005
1006static ssize_t show_temp(struct device *dev, char *buf, int nr)
1007{
1008 struct adm1026_data *data = adm1026_update_device(dev);
1009 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]));
1010}
1011static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
1012{
1013 struct adm1026_data *data = adm1026_update_device(dev);
1014 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]));
1015}
1016static ssize_t set_temp_min(struct device *dev, const char *buf,
1017 size_t count, int nr)
1018{
1019 struct i2c_client *client = to_i2c_client(dev);
1020 struct adm1026_data *data = i2c_get_clientdata(client);
1021 int val;
1022
1023 down(&data->update_lock);
1024 val = simple_strtol(buf, NULL, 10);
1025 data->temp_min[nr] = TEMP_TO_REG(val);
1026 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
1027 data->temp_min[nr]);
1028 up(&data->update_lock);
1029 return count;
1030}
1031static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
1032{
1033 struct adm1026_data *data = adm1026_update_device(dev);
1034 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1035}
1036static ssize_t set_temp_max(struct device *dev, const char *buf,
1037 size_t count, int nr)
1038{
1039 struct i2c_client *client = to_i2c_client(dev);
1040 struct adm1026_data *data = i2c_get_clientdata(client);
1041 int val;
1042
1043 down(&data->update_lock);
1044 val = simple_strtol(buf, NULL, 10);
1045 data->temp_max[nr] = TEMP_TO_REG(val);
1046 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1047 data->temp_max[nr]);
1048 up(&data->update_lock);
1049 return count;
1050}
1051#define temp_reg(offset) \
1052static ssize_t show_temp_##offset (struct device *dev, char *buf) \
1053{ \
1054 return show_temp(dev, buf, offset - 1); \
1055} \
1056static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
1057{ \
1058 return show_temp_min(dev, buf, offset - 1); \
1059} \
1060static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
1061{ \
1062 return show_temp_max(dev, buf, offset - 1); \
1063} \
1064static ssize_t set_temp_##offset##_min (struct device *dev, \
1065 const char *buf, size_t count) \
1066{ \
1067 return set_temp_min(dev, buf, count, offset - 1); \
1068} \
1069static ssize_t set_temp_##offset##_max (struct device *dev, \
1070 const char *buf, size_t count) \
1071{ \
1072 return set_temp_max(dev, buf, count, offset - 1); \
1073} \
1074static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
1075static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1076 show_temp_##offset##_min, set_temp_##offset##_min); \
1077static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1078 show_temp_##offset##_max, set_temp_##offset##_max);
1079
1080
1081temp_reg(1);
1082temp_reg(2);
1083temp_reg(3);
1084
1085static ssize_t show_temp_offset(struct device *dev, char *buf, int nr)
1086{
1087 struct adm1026_data *data = adm1026_update_device(dev);
1088 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1089}
1090static ssize_t set_temp_offset(struct device *dev, const char *buf,
1091 size_t count, int nr)
1092{
1093 struct i2c_client *client = to_i2c_client(dev);
1094 struct adm1026_data *data = i2c_get_clientdata(client);
1095 int val;
1096
1097 down(&data->update_lock);
1098 val = simple_strtol(buf, NULL, 10);
1099 data->temp_offset[nr] = TEMP_TO_REG(val);
1100 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1101 data->temp_offset[nr]);
1102 up(&data->update_lock);
1103 return count;
1104}
1105
1106#define temp_offset_reg(offset) \
1107static ssize_t show_temp_##offset##_offset (struct device *dev, char *buf) \
1108{ \
1109 return show_temp_offset(dev, buf, offset - 1); \
1110} \
1111static ssize_t set_temp_##offset##_offset (struct device *dev, \
1112 const char *buf, size_t count) \
1113{ \
1114 return set_temp_offset(dev, buf, count, offset - 1); \
1115} \
1116static DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1117 show_temp_##offset##_offset, set_temp_##offset##_offset);
1118
1119temp_offset_reg(1);
1120temp_offset_reg(2);
1121temp_offset_reg(3);
1122
1123static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev, char *buf,
1124 int nr)
1125{
1126 struct adm1026_data *data = adm1026_update_device(dev);
1127 return sprintf(buf,"%d\n", TEMP_FROM_REG(
1128 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1129}
1130static ssize_t show_temp_auto_point2_temp(struct device *dev, char *buf,
1131 int nr)
1132{
1133 struct adm1026_data *data = adm1026_update_device(dev);
1134 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1135 ADM1026_FAN_CONTROL_TEMP_RANGE));
1136}
1137static ssize_t show_temp_auto_point1_temp(struct device *dev, char *buf,
1138 int nr)
1139{
1140 struct adm1026_data *data = adm1026_update_device(dev);
1141 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1142}
1143static ssize_t set_temp_auto_point1_temp(struct device *dev, const char *buf,
1144 size_t count, int nr)
1145{
1146 struct i2c_client *client = to_i2c_client(dev);
1147 struct adm1026_data *data = i2c_get_clientdata(client);
1148 int val;
1149
1150 down(&data->update_lock);
1151 val = simple_strtol(buf, NULL, 10);
1152 data->temp_tmin[nr] = TEMP_TO_REG(val);
1153 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1154 data->temp_tmin[nr]);
1155 up(&data->update_lock);
1156 return count;
1157}
1158
1159#define temp_auto_point(offset) \
1160static ssize_t show_temp##offset##_auto_point1_temp (struct device *dev, \
1161 char *buf) \
1162{ \
1163 return show_temp_auto_point1_temp(dev, buf, offset - 1); \
1164} \
1165static ssize_t set_temp##offset##_auto_point1_temp (struct device *dev, \
1166 const char *buf, size_t count) \
1167{ \
1168 return set_temp_auto_point1_temp(dev, buf, count, offset - 1); \
1169} \
1170static ssize_t show_temp##offset##_auto_point1_temp_hyst (struct device \
1171 *dev, char *buf) \
1172{ \
1173 return show_temp_auto_point1_temp_hyst(dev, buf, offset - 1); \
1174} \
1175static ssize_t show_temp##offset##_auto_point2_temp (struct device *dev, \
1176 char *buf) \
1177{ \
1178 return show_temp_auto_point2_temp(dev, buf, offset - 1); \
1179} \
1180static DEVICE_ATTR(temp##offset##_auto_point1_temp, S_IRUGO | S_IWUSR, \
1181 show_temp##offset##_auto_point1_temp, \
1182 set_temp##offset##_auto_point1_temp); \
1183static DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO, \
1184 show_temp##offset##_auto_point1_temp_hyst, NULL); \
1185static DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
1186 show_temp##offset##_auto_point2_temp, NULL);
1187
1188temp_auto_point(1);
1189temp_auto_point(2);
1190temp_auto_point(3);
1191
1192static ssize_t show_temp_crit_enable(struct device *dev, char *buf)
1193{
1194 struct adm1026_data *data = adm1026_update_device(dev);
1195 return sprintf(buf,"%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1196}
1197static ssize_t set_temp_crit_enable(struct device *dev, const char *buf,
1198 size_t count)
1199{
1200 struct i2c_client *client = to_i2c_client(dev);
1201 struct adm1026_data *data = i2c_get_clientdata(client);
1202 int val;
1203
1204 val = simple_strtol(buf, NULL, 10);
1205 if ((val == 1) || (val==0)) {
1206 down(&data->update_lock);
1207 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1208 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1209 data->config1);
1210 up(&data->update_lock);
1211 }
1212 return count;
1213}
1214
1215static DEVICE_ATTR(temp1_crit_enable, S_IRUGO | S_IWUSR,
1216 show_temp_crit_enable, set_temp_crit_enable);
1217
1218static DEVICE_ATTR(temp2_crit_enable, S_IRUGO | S_IWUSR,
1219 show_temp_crit_enable, set_temp_crit_enable);
1220
1221static DEVICE_ATTR(temp3_crit_enable, S_IRUGO | S_IWUSR,
1222 show_temp_crit_enable, set_temp_crit_enable);
1223
1224
1225static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
1226{
1227 struct adm1026_data *data = adm1026_update_device(dev);
1228 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1229}
1230static ssize_t set_temp_crit(struct device *dev, const char *buf,
1231 size_t count, int nr)
1232{
1233 struct i2c_client *client = to_i2c_client(dev);
1234 struct adm1026_data *data = i2c_get_clientdata(client);
1235 int val;
1236
1237 down(&data->update_lock);
1238 val = simple_strtol(buf, NULL, 10);
1239 data->temp_crit[nr] = TEMP_TO_REG(val);
1240 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1241 data->temp_crit[nr]);
1242 up(&data->update_lock);
1243 return count;
1244}
1245
1246#define temp_crit_reg(offset) \
1247static ssize_t show_temp_##offset##_crit (struct device *dev, char *buf) \
1248{ \
1249 return show_temp_crit(dev, buf, offset - 1); \
1250} \
1251static ssize_t set_temp_##offset##_crit (struct device *dev, \
1252 const char *buf, size_t count) \
1253{ \
1254 return set_temp_crit(dev, buf, count, offset - 1); \
1255} \
1256static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1257 show_temp_##offset##_crit, set_temp_##offset##_crit);
1258
1259temp_crit_reg(1);
1260temp_crit_reg(2);
1261temp_crit_reg(3);
1262
1263static ssize_t show_analog_out_reg(struct device *dev, char *buf)
1264{
1265 struct adm1026_data *data = adm1026_update_device(dev);
1266 return sprintf(buf,"%d\n", DAC_FROM_REG(data->analog_out));
1267}
1268static ssize_t set_analog_out_reg(struct device *dev, const char *buf,
1269 size_t count)
1270{
1271 struct i2c_client *client = to_i2c_client(dev);
1272 struct adm1026_data *data = i2c_get_clientdata(client);
1273 int val;
1274
1275 down(&data->update_lock);
1276 val = simple_strtol(buf, NULL, 10);
1277 data->analog_out = DAC_TO_REG(val);
1278 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1279 up(&data->update_lock);
1280 return count;
1281}
1282
1283static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
1284 set_analog_out_reg);
1285
1286static ssize_t show_vid_reg(struct device *dev, char *buf)
1287{
1288 struct adm1026_data *data = adm1026_update_device(dev);
1289 return sprintf(buf,"%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
1290}
1291
1292static DEVICE_ATTR(vid, S_IRUGO, show_vid_reg, NULL);
1293
1294static ssize_t show_vrm_reg(struct device *dev, char *buf)
1295{
1296 struct adm1026_data *data = adm1026_update_device(dev);
1297 return sprintf(buf,"%d\n", data->vrm);
1298}
1299static ssize_t store_vrm_reg(struct device *dev, const char *buf,
1300 size_t count)
1301{
1302 struct i2c_client *client = to_i2c_client(dev);
1303 struct adm1026_data *data = i2c_get_clientdata(client);
1304
1305 data->vrm = simple_strtol(buf, NULL, 10);
1306 return count;
1307}
1308
1309static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1310
1311static ssize_t show_alarms_reg(struct device *dev, char *buf)
1312{
1313 struct adm1026_data *data = adm1026_update_device(dev);
1314 return sprintf(buf, "%ld\n", (long) (data->alarms));
1315}
1316
1317static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1318
1319static ssize_t show_alarm_mask(struct device *dev, char *buf)
1320{
1321 struct adm1026_data *data = adm1026_update_device(dev);
1322 return sprintf(buf,"%ld\n", data->alarm_mask);
1323}
1324static ssize_t set_alarm_mask(struct device *dev, const char *buf,
1325 size_t count)
1326{
1327 struct i2c_client *client = to_i2c_client(dev);
1328 struct adm1026_data *data = i2c_get_clientdata(client);
1329 int val;
1330 unsigned long mask;
1331
1332 down(&data->update_lock);
1333 val = simple_strtol(buf, NULL, 10);
1334 data->alarm_mask = val & 0x7fffffff;
1335 mask = data->alarm_mask
1336 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1337 adm1026_write_value(client, ADM1026_REG_MASK1,
1338 mask & 0xff);
1339 mask >>= 8;
1340 adm1026_write_value(client, ADM1026_REG_MASK2,
1341 mask & 0xff);
1342 mask >>= 8;
1343 adm1026_write_value(client, ADM1026_REG_MASK3,
1344 mask & 0xff);
1345 mask >>= 8;
1346 adm1026_write_value(client, ADM1026_REG_MASK4,
1347 mask & 0xff);
1348 up(&data->update_lock);
1349 return count;
1350}
1351
1352static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1353 set_alarm_mask);
1354
1355
1356static ssize_t show_gpio(struct device *dev, char *buf)
1357{
1358 struct adm1026_data *data = adm1026_update_device(dev);
1359 return sprintf(buf,"%ld\n", data->gpio);
1360}
1361static ssize_t set_gpio(struct device *dev, const char *buf,
1362 size_t count)
1363{
1364 struct i2c_client *client = to_i2c_client(dev);
1365 struct adm1026_data *data = i2c_get_clientdata(client);
1366 int val;
1367 long gpio;
1368
1369 down(&data->update_lock);
1370 val = simple_strtol(buf, NULL, 10);
1371 data->gpio = val & 0x1ffff;
1372 gpio = data->gpio;
1373 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7,gpio & 0xff);
1374 gpio >>= 8;
1375 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15,gpio & 0xff);
1376 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1377 adm1026_write_value(client, ADM1026_REG_STATUS4,gpio & 0xff);
1378 up(&data->update_lock);
1379 return count;
1380}
1381
1382static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1383
1384
1385static ssize_t show_gpio_mask(struct device *dev, char *buf)
1386{
1387 struct adm1026_data *data = adm1026_update_device(dev);
1388 return sprintf(buf,"%ld\n", data->gpio_mask);
1389}
1390static ssize_t set_gpio_mask(struct device *dev, const char *buf,
1391 size_t count)
1392{
1393 struct i2c_client *client = to_i2c_client(dev);
1394 struct adm1026_data *data = i2c_get_clientdata(client);
1395 int val;
1396 long mask;
1397
1398 down(&data->update_lock);
1399 val = simple_strtol(buf, NULL, 10);
1400 data->gpio_mask = val & 0x1ffff;
1401 mask = data->gpio_mask;
1402 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7,mask & 0xff);
1403 mask >>= 8;
1404 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15,mask & 0xff);
1405 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1406 adm1026_write_value(client, ADM1026_REG_MASK1,mask & 0xff);
1407 up(&data->update_lock);
1408 return count;
1409}
1410
1411static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1412
1413static ssize_t show_pwm_reg(struct device *dev, char *buf)
1414{
1415 struct adm1026_data *data = adm1026_update_device(dev);
1416 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm1.pwm));
1417}
1418static ssize_t set_pwm_reg(struct device *dev, const char *buf,
1419 size_t count)
1420{
1421 struct i2c_client *client = to_i2c_client(dev);
1422 struct adm1026_data *data = i2c_get_clientdata(client);
1423 int val;
1424
1425 if (data->pwm1.enable == 1) {
1426 down(&data->update_lock);
1427 val = simple_strtol(buf, NULL, 10);
1428 data->pwm1.pwm = PWM_TO_REG(val);
1429 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1430 up(&data->update_lock);
1431 }
1432 return count;
1433}
1434static ssize_t show_auto_pwm_min(struct device *dev, char *buf)
1435{
1436 struct adm1026_data *data = adm1026_update_device(dev);
1437 return sprintf(buf,"%d\n", data->pwm1.auto_pwm_min);
1438}
1439static ssize_t set_auto_pwm_min(struct device *dev, const char *buf,
1440 size_t count)
1441{
1442 struct i2c_client *client = to_i2c_client(dev);
1443 struct adm1026_data *data = i2c_get_clientdata(client);
1444 int val;
1445
1446 down(&data->update_lock);
1447 val = simple_strtol(buf, NULL, 10);
1448 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val,0,255);
1449 if (data->pwm1.enable == 2) {
1450 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1451 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1452 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1453 }
1454 up(&data->update_lock);
1455 return count;
1456}
1457static ssize_t show_auto_pwm_max(struct device *dev, char *buf)
1458{
1459 return sprintf(buf,"%d\n", ADM1026_PWM_MAX);
1460}
1461static ssize_t show_pwm_enable(struct device *dev, char *buf)
1462{
1463 struct adm1026_data *data = adm1026_update_device(dev);
1464 return sprintf(buf,"%d\n", data->pwm1.enable);
1465}
1466static ssize_t set_pwm_enable(struct device *dev, const char *buf,
1467 size_t count)
1468{
1469 struct i2c_client *client = to_i2c_client(dev);
1470 struct adm1026_data *data = i2c_get_clientdata(client);
1471 int val;
1472 int old_enable;
1473
1474 val = simple_strtol(buf, NULL, 10);
1475 if ((val >= 0) && (val < 3)) {
1476 down(&data->update_lock);
1477 old_enable = data->pwm1.enable;
1478 data->pwm1.enable = val;
1479 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1480 | ((val == 2) ? CFG1_PWM_AFC : 0);
1481 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1482 data->config1);
1483 if (val == 2) {
1484 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1485 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1486 adm1026_write_value(client, ADM1026_REG_PWM,
1487 data->pwm1.pwm);
1488 } else if (!((old_enable == 1) && (val == 1))) {
1489
1490 data->pwm1.pwm = 255;
1491 adm1026_write_value(client, ADM1026_REG_PWM,
1492 data->pwm1.pwm);
1493 }
1494 up(&data->update_lock);
1495 }
1496 return count;
1497}
1498
1499
1500static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1501static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1502static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1503static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1504 set_pwm_enable);
1505static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1506 set_pwm_enable);
1507static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1508 set_pwm_enable);
1509static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1510 show_auto_pwm_min, set_auto_pwm_min);
1511static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1512 show_auto_pwm_min, set_auto_pwm_min);
1513static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1514 show_auto_pwm_min, set_auto_pwm_min);
1515
1516static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1517static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1518static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1519
1520int adm1026_detect(struct i2c_adapter *adapter, int address,
1521 int kind)
1522{
1523 int company, verstep;
1524 struct i2c_client *new_client;
1525 struct adm1026_data *data;
1526 int err = 0;
1527 const char *type_name = "";
1528
1529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1530
1531 goto exit;
1532 };
1533
1534
1535
1536
1537
1538 if (!(data = kmalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
1539 err = -ENOMEM;
1540 goto exit;
1541 }
1542
1543 memset(data, 0, sizeof(struct adm1026_data));
1544
1545 new_client = &data->client;
1546 i2c_set_clientdata(new_client, data);
1547 new_client->addr = address;
1548 new_client->adapter = adapter;
1549 new_client->driver = &adm1026_driver;
1550 new_client->flags = 0;
1551
1552
1553
1554 company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
1555 verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
1556
1557 dev_dbg(&new_client->dev, "Detecting device at %d,0x%02x with"
1558 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1559 i2c_adapter_id(new_client->adapter), new_client->addr,
1560 company, verstep);
1561
1562
1563 if (kind <= 0) {
1564 dev_dbg(&new_client->dev, "Autodetecting device at %d,0x%02x "
1565 "...\n", i2c_adapter_id(adapter), address);
1566 if (company == ADM1026_COMPANY_ANALOG_DEV
1567 && verstep == ADM1026_VERSTEP_ADM1026) {
1568 kind = adm1026;
1569 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1570 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1571 dev_err(&adapter->dev, ": Unrecognized stepping "
1572 "0x%02x. Defaulting to ADM1026.\n", verstep);
1573 kind = adm1026;
1574 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1575 dev_err(&adapter->dev, ": Found version/stepping "
1576 "0x%02x. Assuming generic ADM1026.\n",
1577 verstep);
1578 kind = any_chip;
1579 } else {
1580 dev_dbg(&new_client->dev, ": Autodetection "
1581 "failed\n");
1582
1583 if (kind == 0) {
1584 dev_err(&adapter->dev, "Generic ADM1026 not "
1585 "found at %d,0x%02x. Try "
1586 "force_adm1026.\n",
1587 i2c_adapter_id(adapter), address);
1588 }
1589 err = 0;
1590 goto exitfree;
1591 }
1592 }
1593
1594
1595 switch (kind) {
1596 case any_chip :
1597 type_name = "adm1026";
1598 break;
1599 case adm1026 :
1600 type_name = "adm1026";
1601 break;
1602 default :
1603 dev_err(&adapter->dev, ": Internal error, invalid "
1604 "kind (%d)!", kind);
1605 err = -EFAULT;
1606 goto exitfree;
1607 }
1608 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1609
1610
1611 new_client->id = adm1026_id++;
1612 data->type = kind;
1613 data->valid = 0;
1614 init_MUTEX(&data->update_lock);
1615
1616 dev_dbg(&new_client->dev, "(%d): Assigning ID %d to %s at %d,0x%02x\n",
1617 new_client->id, new_client->id, new_client->name,
1618 i2c_adapter_id(new_client->adapter),
1619 new_client->addr);
1620
1621
1622 if ((err = i2c_attach_client(new_client)))
1623 goto exitfree;
1624
1625
1626 data->vrm = i2c_which_vrm();
1627
1628
1629 adm1026_init_client(new_client);
1630
1631
1632 device_create_file(&new_client->dev, &dev_attr_in0_input);
1633 device_create_file(&new_client->dev, &dev_attr_in0_max);
1634 device_create_file(&new_client->dev, &dev_attr_in0_min);
1635 device_create_file(&new_client->dev, &dev_attr_in1_input);
1636 device_create_file(&new_client->dev, &dev_attr_in1_max);
1637 device_create_file(&new_client->dev, &dev_attr_in1_min);
1638 device_create_file(&new_client->dev, &dev_attr_in2_input);
1639 device_create_file(&new_client->dev, &dev_attr_in2_max);
1640 device_create_file(&new_client->dev, &dev_attr_in2_min);
1641 device_create_file(&new_client->dev, &dev_attr_in3_input);
1642 device_create_file(&new_client->dev, &dev_attr_in3_max);
1643 device_create_file(&new_client->dev, &dev_attr_in3_min);
1644 device_create_file(&new_client->dev, &dev_attr_in4_input);
1645 device_create_file(&new_client->dev, &dev_attr_in4_max);
1646 device_create_file(&new_client->dev, &dev_attr_in4_min);
1647 device_create_file(&new_client->dev, &dev_attr_in5_input);
1648 device_create_file(&new_client->dev, &dev_attr_in5_max);
1649 device_create_file(&new_client->dev, &dev_attr_in5_min);
1650 device_create_file(&new_client->dev, &dev_attr_in6_input);
1651 device_create_file(&new_client->dev, &dev_attr_in6_max);
1652 device_create_file(&new_client->dev, &dev_attr_in6_min);
1653 device_create_file(&new_client->dev, &dev_attr_in7_input);
1654 device_create_file(&new_client->dev, &dev_attr_in7_max);
1655 device_create_file(&new_client->dev, &dev_attr_in7_min);
1656 device_create_file(&new_client->dev, &dev_attr_in8_input);
1657 device_create_file(&new_client->dev, &dev_attr_in8_max);
1658 device_create_file(&new_client->dev, &dev_attr_in8_min);
1659 device_create_file(&new_client->dev, &dev_attr_in9_input);
1660 device_create_file(&new_client->dev, &dev_attr_in9_max);
1661 device_create_file(&new_client->dev, &dev_attr_in9_min);
1662 device_create_file(&new_client->dev, &dev_attr_in10_input);
1663 device_create_file(&new_client->dev, &dev_attr_in10_max);
1664 device_create_file(&new_client->dev, &dev_attr_in10_min);
1665 device_create_file(&new_client->dev, &dev_attr_in11_input);
1666 device_create_file(&new_client->dev, &dev_attr_in11_max);
1667 device_create_file(&new_client->dev, &dev_attr_in11_min);
1668 device_create_file(&new_client->dev, &dev_attr_in12_input);
1669 device_create_file(&new_client->dev, &dev_attr_in12_max);
1670 device_create_file(&new_client->dev, &dev_attr_in12_min);
1671 device_create_file(&new_client->dev, &dev_attr_in13_input);
1672 device_create_file(&new_client->dev, &dev_attr_in13_max);
1673 device_create_file(&new_client->dev, &dev_attr_in13_min);
1674 device_create_file(&new_client->dev, &dev_attr_in14_input);
1675 device_create_file(&new_client->dev, &dev_attr_in14_max);
1676 device_create_file(&new_client->dev, &dev_attr_in14_min);
1677 device_create_file(&new_client->dev, &dev_attr_in15_input);
1678 device_create_file(&new_client->dev, &dev_attr_in15_max);
1679 device_create_file(&new_client->dev, &dev_attr_in15_min);
1680 device_create_file(&new_client->dev, &dev_attr_in16_input);
1681 device_create_file(&new_client->dev, &dev_attr_in16_max);
1682 device_create_file(&new_client->dev, &dev_attr_in16_min);
1683 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1684 device_create_file(&new_client->dev, &dev_attr_fan1_div);
1685 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1686 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1687 device_create_file(&new_client->dev, &dev_attr_fan2_div);
1688 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1689 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1690 device_create_file(&new_client->dev, &dev_attr_fan3_div);
1691 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1692 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1693 device_create_file(&new_client->dev, &dev_attr_fan4_div);
1694 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1695 device_create_file(&new_client->dev, &dev_attr_fan5_input);
1696 device_create_file(&new_client->dev, &dev_attr_fan5_div);
1697 device_create_file(&new_client->dev, &dev_attr_fan5_min);
1698 device_create_file(&new_client->dev, &dev_attr_fan6_input);
1699 device_create_file(&new_client->dev, &dev_attr_fan6_div);
1700 device_create_file(&new_client->dev, &dev_attr_fan6_min);
1701 device_create_file(&new_client->dev, &dev_attr_fan7_input);
1702 device_create_file(&new_client->dev, &dev_attr_fan7_div);
1703 device_create_file(&new_client->dev, &dev_attr_fan7_min);
1704 device_create_file(&new_client->dev, &dev_attr_fan8_input);
1705 device_create_file(&new_client->dev, &dev_attr_fan8_div);
1706 device_create_file(&new_client->dev, &dev_attr_fan8_min);
1707 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1708 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1709 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1710 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1711 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1712 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1713 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1714 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1715 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1716 device_create_file(&new_client->dev, &dev_attr_temp1_offset);
1717 device_create_file(&new_client->dev, &dev_attr_temp2_offset);
1718 device_create_file(&new_client->dev, &dev_attr_temp3_offset);
1719 device_create_file(&new_client->dev,
1720 &dev_attr_temp1_auto_point1_temp);
1721 device_create_file(&new_client->dev,
1722 &dev_attr_temp2_auto_point1_temp);
1723 device_create_file(&new_client->dev,
1724 &dev_attr_temp3_auto_point1_temp);
1725 device_create_file(&new_client->dev,
1726 &dev_attr_temp1_auto_point1_temp_hyst);
1727 device_create_file(&new_client->dev,
1728 &dev_attr_temp2_auto_point1_temp_hyst);
1729 device_create_file(&new_client->dev,
1730 &dev_attr_temp3_auto_point1_temp_hyst);
1731 device_create_file(&new_client->dev,
1732 &dev_attr_temp1_auto_point2_temp);
1733 device_create_file(&new_client->dev,
1734 &dev_attr_temp2_auto_point2_temp);
1735 device_create_file(&new_client->dev,
1736 &dev_attr_temp3_auto_point2_temp);
1737 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
1738 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
1739 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
1740 device_create_file(&new_client->dev, &dev_attr_temp1_crit_enable);
1741 device_create_file(&new_client->dev, &dev_attr_temp2_crit_enable);
1742 device_create_file(&new_client->dev, &dev_attr_temp3_crit_enable);
1743 device_create_file(&new_client->dev, &dev_attr_vid);
1744 device_create_file(&new_client->dev, &dev_attr_vrm);
1745 device_create_file(&new_client->dev, &dev_attr_alarms);
1746 device_create_file(&new_client->dev, &dev_attr_alarm_mask);
1747 device_create_file(&new_client->dev, &dev_attr_gpio);
1748 device_create_file(&new_client->dev, &dev_attr_gpio_mask);
1749 device_create_file(&new_client->dev, &dev_attr_pwm1);
1750 device_create_file(&new_client->dev, &dev_attr_pwm2);
1751 device_create_file(&new_client->dev, &dev_attr_pwm3);
1752 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1753 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1754 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1755 device_create_file(&new_client->dev, &dev_attr_temp1_auto_point1_pwm);
1756 device_create_file(&new_client->dev, &dev_attr_temp2_auto_point1_pwm);
1757 device_create_file(&new_client->dev, &dev_attr_temp3_auto_point1_pwm);
1758 device_create_file(&new_client->dev, &dev_attr_temp1_auto_point2_pwm);
1759 device_create_file(&new_client->dev, &dev_attr_temp2_auto_point2_pwm);
1760 device_create_file(&new_client->dev, &dev_attr_temp3_auto_point2_pwm);
1761 device_create_file(&new_client->dev, &dev_attr_analog_out);
1762 return 0;
1763
1764
1765exitfree:
1766 kfree(new_client);
1767exit:
1768 return err;
1769}
1770static int __init sm_adm1026_init(void)
1771{
1772 return i2c_add_driver(&adm1026_driver);
1773}
1774
1775static void __exit sm_adm1026_exit(void)
1776{
1777 i2c_del_driver(&adm1026_driver);
1778}
1779
1780MODULE_LICENSE("GPL");
1781MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1782 "Justin Thiessen <jthiessen@penguincomputing.com>");
1783MODULE_DESCRIPTION("ADM1026 driver");
1784
1785module_init(sm_adm1026_init);
1786module_exit(sm_adm1026_exit);
1787