2 * A driver for the Integrated Circuits ICS932S401
3 * Copyright (C) 2008 IBM
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/delay.h>
28 #include <linux/log2.h>
30 /* Addresses to scan */
31 static const unsigned short normal_i2c
[] = { 0x69, I2C_CLIENT_END
};
33 /* ICS932S401 registers */
34 #define ICS932S401_REG_CFG2 0x01
35 #define ICS932S401_CFG1_SPREAD 0x01
36 #define ICS932S401_REG_CFG7 0x06
37 #define ICS932S401_FS_MASK 0x07
38 #define ICS932S401_REG_VENDOR_REV 0x07
39 #define ICS932S401_VENDOR 1
40 #define ICS932S401_VENDOR_MASK 0x0F
41 #define ICS932S401_REV 4
42 #define ICS932S401_REV_SHIFT 4
43 #define ICS932S401_REG_DEVICE 0x09
44 #define ICS932S401_DEVICE 11
45 #define ICS932S401_REG_CTRL 0x0A
46 #define ICS932S401_MN_ENABLED 0x80
47 #define ICS932S401_CPU_ALT 0x04
48 #define ICS932S401_SRC_ALT 0x08
49 #define ICS932S401_REG_CPU_M_CTRL 0x0B
50 #define ICS932S401_M_MASK 0x3F
51 #define ICS932S401_REG_CPU_N_CTRL 0x0C
52 #define ICS932S401_REG_CPU_SPREAD1 0x0D
53 #define ICS932S401_REG_CPU_SPREAD2 0x0E
54 #define ICS932S401_SPREAD_MASK 0x7FFF
55 #define ICS932S401_REG_SRC_M_CTRL 0x0F
56 #define ICS932S401_REG_SRC_N_CTRL 0x10
57 #define ICS932S401_REG_SRC_SPREAD1 0x11
58 #define ICS932S401_REG_SRC_SPREAD2 0x12
59 #define ICS932S401_REG_CPU_DIVISOR 0x13
60 #define ICS932S401_CPU_DIVISOR_SHIFT 4
61 #define ICS932S401_REG_PCISRC_DIVISOR 0x14
62 #define ICS932S401_SRC_DIVISOR_MASK 0x0F
63 #define ICS932S401_PCI_DIVISOR_SHIFT 4
65 /* Base clock is 14.318MHz */
66 #define BASE_CLOCK 14318
69 #define NUM_MIRRORED_REGS 15
71 static int regs_to_copy
[NUM_MIRRORED_REGS
] = {
74 ICS932S401_REG_VENDOR_REV
,
75 ICS932S401_REG_DEVICE
,
77 ICS932S401_REG_CPU_M_CTRL
,
78 ICS932S401_REG_CPU_N_CTRL
,
79 ICS932S401_REG_CPU_SPREAD1
,
80 ICS932S401_REG_CPU_SPREAD2
,
81 ICS932S401_REG_SRC_M_CTRL
,
82 ICS932S401_REG_SRC_N_CTRL
,
83 ICS932S401_REG_SRC_SPREAD1
,
84 ICS932S401_REG_SRC_SPREAD2
,
85 ICS932S401_REG_CPU_DIVISOR
,
86 ICS932S401_REG_PCISRC_DIVISOR
,
89 /* How often do we reread sensors values? (In jiffies) */
90 #define SENSOR_REFRESH_INTERVAL (2 * HZ)
92 /* How often do we reread sensor limit values? (In jiffies) */
93 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
95 struct ics932s401_data
{
96 struct attribute_group attrs
;
99 unsigned long sensors_last_updated
; /* In jiffies */
104 static int ics932s401_probe(struct i2c_client
*client
,
105 const struct i2c_device_id
*id
);
106 static int ics932s401_detect(struct i2c_client
*client
,
107 struct i2c_board_info
*info
);
108 static int ics932s401_remove(struct i2c_client
*client
);
110 static const struct i2c_device_id ics932s401_id
[] = {
114 MODULE_DEVICE_TABLE(i2c
, ics932s401_id
);
116 static struct i2c_driver ics932s401_driver
= {
117 .class = I2C_CLASS_HWMON
,
119 .name
= "ics932s401",
121 .probe
= ics932s401_probe
,
122 .remove
= ics932s401_remove
,
123 .id_table
= ics932s401_id
,
124 .detect
= ics932s401_detect
,
125 .address_list
= normal_i2c
,
128 static struct ics932s401_data
*ics932s401_update_device(struct device
*dev
)
130 struct i2c_client
*client
= to_i2c_client(dev
);
131 struct ics932s401_data
*data
= i2c_get_clientdata(client
);
132 unsigned long local_jiffies
= jiffies
;
135 mutex_lock(&data
->lock
);
136 if (time_before(local_jiffies
, data
->sensors_last_updated
+
137 SENSOR_REFRESH_INTERVAL
)
138 && data
->sensors_valid
)
142 * Each register must be read as a word and then right shifted 8 bits.
143 * Not really sure why this is; setting the "byte count programming"
144 * register to 1 does not fix this problem.
146 for (i
= 0; i
< NUM_MIRRORED_REGS
; i
++) {
147 temp
= i2c_smbus_read_word_data(client
, regs_to_copy
[i
]);
148 data
->regs
[regs_to_copy
[i
]] = temp
>> 8;
151 data
->sensors_last_updated
= local_jiffies
;
152 data
->sensors_valid
= 1;
155 mutex_unlock(&data
->lock
);
159 static ssize_t
show_spread_enabled(struct device
*dev
,
160 struct device_attribute
*devattr
,
163 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
165 if (data
->regs
[ICS932S401_REG_CFG2
] & ICS932S401_CFG1_SPREAD
)
166 return sprintf(buf
, "1\n");
168 return sprintf(buf
, "0\n");
171 /* bit to cpu khz map */
172 static const int fs_speeds
[] = {
183 /* clock divisor map */
184 static const int divisors
[] = {2, 3, 5, 15, 4, 6, 10, 30, 8, 12, 20, 60, 16,
187 /* Calculate CPU frequency from the M/N registers. */
188 static int calculate_cpu_freq(struct ics932s401_data
*data
)
192 m
= data
->regs
[ICS932S401_REG_CPU_M_CTRL
] & ICS932S401_M_MASK
;
193 n
= data
->regs
[ICS932S401_REG_CPU_N_CTRL
];
195 /* Pull in bits 8 & 9 from the M register */
196 n
|= ((int)data
->regs
[ICS932S401_REG_CPU_M_CTRL
] & 0x80) << 1;
197 n
|= ((int)data
->regs
[ICS932S401_REG_CPU_M_CTRL
] & 0x40) << 3;
199 freq
= BASE_CLOCK
* (n
+ 8) / (m
+ 2);
200 freq
/= divisors
[data
->regs
[ICS932S401_REG_CPU_DIVISOR
] >>
201 ICS932S401_CPU_DIVISOR_SHIFT
];
206 static ssize_t
show_cpu_clock(struct device
*dev
,
207 struct device_attribute
*devattr
,
210 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
212 return sprintf(buf
, "%d\n", calculate_cpu_freq(data
));
215 static ssize_t
show_cpu_clock_sel(struct device
*dev
,
216 struct device_attribute
*devattr
,
219 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
222 if (data
->regs
[ICS932S401_REG_CTRL
] & ICS932S401_MN_ENABLED
)
223 freq
= calculate_cpu_freq(data
);
225 /* Freq is neatly wrapped up for us */
226 int fid
= data
->regs
[ICS932S401_REG_CFG7
] & ICS932S401_FS_MASK
;
227 freq
= fs_speeds
[fid
];
228 if (data
->regs
[ICS932S401_REG_CTRL
] & ICS932S401_CPU_ALT
) {
240 return sprintf(buf
, "%d\n", freq
);
243 /* Calculate SRC frequency from the M/N registers. */
244 static int calculate_src_freq(struct ics932s401_data
*data
)
248 m
= data
->regs
[ICS932S401_REG_SRC_M_CTRL
] & ICS932S401_M_MASK
;
249 n
= data
->regs
[ICS932S401_REG_SRC_N_CTRL
];
251 /* Pull in bits 8 & 9 from the M register */
252 n
|= ((int)data
->regs
[ICS932S401_REG_SRC_M_CTRL
] & 0x80) << 1;
253 n
|= ((int)data
->regs
[ICS932S401_REG_SRC_M_CTRL
] & 0x40) << 3;
255 freq
= BASE_CLOCK
* (n
+ 8) / (m
+ 2);
256 freq
/= divisors
[data
->regs
[ICS932S401_REG_PCISRC_DIVISOR
] &
257 ICS932S401_SRC_DIVISOR_MASK
];
262 static ssize_t
show_src_clock(struct device
*dev
,
263 struct device_attribute
*devattr
,
266 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
268 return sprintf(buf
, "%d\n", calculate_src_freq(data
));
271 static ssize_t
show_src_clock_sel(struct device
*dev
,
272 struct device_attribute
*devattr
,
275 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
278 if (data
->regs
[ICS932S401_REG_CTRL
] & ICS932S401_MN_ENABLED
)
279 freq
= calculate_src_freq(data
);
281 /* Freq is neatly wrapped up for us */
282 if (data
->regs
[ICS932S401_REG_CTRL
] & ICS932S401_CPU_ALT
&&
283 data
->regs
[ICS932S401_REG_CTRL
] & ICS932S401_SRC_ALT
)
288 return sprintf(buf
, "%d\n", freq
);
291 /* Calculate PCI frequency from the SRC M/N registers. */
292 static int calculate_pci_freq(struct ics932s401_data
*data
)
296 m
= data
->regs
[ICS932S401_REG_SRC_M_CTRL
] & ICS932S401_M_MASK
;
297 n
= data
->regs
[ICS932S401_REG_SRC_N_CTRL
];
299 /* Pull in bits 8 & 9 from the M register */
300 n
|= ((int)data
->regs
[ICS932S401_REG_SRC_M_CTRL
] & 0x80) << 1;
301 n
|= ((int)data
->regs
[ICS932S401_REG_SRC_M_CTRL
] & 0x40) << 3;
303 freq
= BASE_CLOCK
* (n
+ 8) / (m
+ 2);
304 freq
/= divisors
[data
->regs
[ICS932S401_REG_PCISRC_DIVISOR
] >>
305 ICS932S401_PCI_DIVISOR_SHIFT
];
310 static ssize_t
show_pci_clock(struct device
*dev
,
311 struct device_attribute
*devattr
,
314 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
316 return sprintf(buf
, "%d\n", calculate_pci_freq(data
));
319 static ssize_t
show_pci_clock_sel(struct device
*dev
,
320 struct device_attribute
*devattr
,
323 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
326 if (data
->regs
[ICS932S401_REG_CTRL
] & ICS932S401_MN_ENABLED
)
327 freq
= calculate_pci_freq(data
);
331 return sprintf(buf
, "%d\n", freq
);
334 static ssize_t
show_value(struct device
*dev
,
335 struct device_attribute
*devattr
,
338 static ssize_t
show_spread(struct device
*dev
,
339 struct device_attribute
*devattr
,
342 static DEVICE_ATTR(spread_enabled
, S_IRUGO
, show_spread_enabled
, NULL
);
343 static DEVICE_ATTR(cpu_clock_selection
, S_IRUGO
, show_cpu_clock_sel
, NULL
);
344 static DEVICE_ATTR(cpu_clock
, S_IRUGO
, show_cpu_clock
, NULL
);
345 static DEVICE_ATTR(src_clock_selection
, S_IRUGO
, show_src_clock_sel
, NULL
);
346 static DEVICE_ATTR(src_clock
, S_IRUGO
, show_src_clock
, NULL
);
347 static DEVICE_ATTR(pci_clock_selection
, S_IRUGO
, show_pci_clock_sel
, NULL
);
348 static DEVICE_ATTR(pci_clock
, S_IRUGO
, show_pci_clock
, NULL
);
349 static DEVICE_ATTR(usb_clock
, S_IRUGO
, show_value
, NULL
);
350 static DEVICE_ATTR(ref_clock
, S_IRUGO
, show_value
, NULL
);
351 static DEVICE_ATTR(cpu_spread
, S_IRUGO
, show_spread
, NULL
);
352 static DEVICE_ATTR(src_spread
, S_IRUGO
, show_spread
, NULL
);
354 static struct attribute
*ics932s401_attr
[] =
356 &dev_attr_spread_enabled
.attr
,
357 &dev_attr_cpu_clock_selection
.attr
,
358 &dev_attr_cpu_clock
.attr
,
359 &dev_attr_src_clock_selection
.attr
,
360 &dev_attr_src_clock
.attr
,
361 &dev_attr_pci_clock_selection
.attr
,
362 &dev_attr_pci_clock
.attr
,
363 &dev_attr_usb_clock
.attr
,
364 &dev_attr_ref_clock
.attr
,
365 &dev_attr_cpu_spread
.attr
,
366 &dev_attr_src_spread
.attr
,
370 static ssize_t
show_value(struct device
*dev
,
371 struct device_attribute
*devattr
,
376 if (devattr
== &dev_attr_usb_clock
)
378 else if (devattr
== &dev_attr_ref_clock
)
383 return sprintf(buf
, "%d\n", x
);
386 static ssize_t
show_spread(struct device
*dev
,
387 struct device_attribute
*devattr
,
390 struct ics932s401_data
*data
= ics932s401_update_device(dev
);
394 if (!(data
->regs
[ICS932S401_REG_CFG2
] & ICS932S401_CFG1_SPREAD
))
395 return sprintf(buf
, "0%%\n");
397 if (devattr
== &dev_attr_src_spread
)
398 reg
= ICS932S401_REG_SRC_SPREAD1
;
399 else if (devattr
== &dev_attr_cpu_spread
)
400 reg
= ICS932S401_REG_CPU_SPREAD1
;
404 val
= data
->regs
[reg
] | (data
->regs
[reg
+ 1] << 8);
405 val
&= ICS932S401_SPREAD_MASK
;
407 /* Scale 0..2^14 to -0.5. */
408 val
= 500000 * val
/ 16384;
409 return sprintf(buf
, "-0.%lu%%\n", val
);
412 /* Return 0 if detection is successful, -ENODEV otherwise */
413 static int ics932s401_detect(struct i2c_client
*client
,
414 struct i2c_board_info
*info
)
416 struct i2c_adapter
*adapter
= client
->adapter
;
417 int vendor
, device
, revision
;
419 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
422 vendor
= i2c_smbus_read_word_data(client
, ICS932S401_REG_VENDOR_REV
);
424 revision
= vendor
>> ICS932S401_REV_SHIFT
;
425 vendor
&= ICS932S401_VENDOR_MASK
;
426 if (vendor
!= ICS932S401_VENDOR
)
429 device
= i2c_smbus_read_word_data(client
, ICS932S401_REG_DEVICE
);
431 if (device
!= ICS932S401_DEVICE
)
434 if (revision
!= ICS932S401_REV
)
435 dev_info(&adapter
->dev
, "Unknown revision %d\n", revision
);
437 strlcpy(info
->type
, "ics932s401", I2C_NAME_SIZE
);
442 static int ics932s401_probe(struct i2c_client
*client
,
443 const struct i2c_device_id
*id
)
445 struct ics932s401_data
*data
;
448 data
= kzalloc(sizeof(struct ics932s401_data
), GFP_KERNEL
);
454 i2c_set_clientdata(client
, data
);
455 mutex_init(&data
->lock
);
457 dev_info(&client
->dev
, "%s chip found\n", client
->name
);
459 /* Register sysfs hooks */
460 data
->attrs
.attrs
= ics932s401_attr
;
461 err
= sysfs_create_group(&client
->dev
.kobj
, &data
->attrs
);
473 static int ics932s401_remove(struct i2c_client
*client
)
475 struct ics932s401_data
*data
= i2c_get_clientdata(client
);
477 sysfs_remove_group(&client
->dev
.kobj
, &data
->attrs
);
482 static int __init
ics932s401_init(void)
484 return i2c_add_driver(&ics932s401_driver
);
487 static void __exit
ics932s401_exit(void)
489 i2c_del_driver(&ics932s401_driver
);
492 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
493 MODULE_DESCRIPTION("ICS932S401 driver");
494 MODULE_LICENSE("GPL");
496 module_init(ics932s401_init
);
497 module_exit(ics932s401_exit
);
499 /* IBM IntelliStation Z30 */
500 MODULE_ALIAS("dmi:bvnIBM:*:rn9228:*");
501 MODULE_ALIAS("dmi:bvnIBM:*:rn9232:*");
503 /* IBM x3650/x3550 */
504 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650*");
505 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550*");