]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/hwmon/i5k_amb.c
hwmon: (i5k_amb) New memory temperature sensor driver
[linux-2.6-omap-h63xx.git] / drivers / hwmon / i5k_amb.c
1 /*
2  * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
3  * temperature sensors
4  * Copyright (C) 2007 IBM
5  *
6  * Author: Darrick J. Wong <djwong@us.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/jiffies.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/log2.h>
31 #include <linux/pci.h>
32 #include <linux/platform_device.h>
33
34 #define DRVNAME "i5k_amb"
35
36 #define I5K_REG_AMB_BASE_ADDR           0x48
37 #define I5K_REG_AMB_LEN_ADDR            0x50
38 #define I5K_REG_CHAN0_PRESENCE_ADDR     0x64
39 #define I5K_REG_CHAN1_PRESENCE_ADDR     0x66
40
41 #define AMB_REG_TEMP_MIN_ADDR           0x80
42 #define AMB_REG_TEMP_MID_ADDR           0x81
43 #define AMB_REG_TEMP_MAX_ADDR           0x82
44 #define AMB_REG_TEMP_STATUS_ADDR        0x84
45 #define AMB_REG_TEMP_ADDR               0x85
46
47 #define AMB_CONFIG_SIZE                 2048
48 #define AMB_FUNC_3_OFFSET               768
49
50 #define AMB_REG_TEMP_STATUS(amb)        ((amb) * AMB_CONFIG_SIZE + \
51                         AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR)
52 #define AMB_REG_TEMP_MIN(amb)           ((amb) * AMB_CONFIG_SIZE + \
53                         AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR)
54 #define AMB_REG_TEMP_MID(amb)           ((amb) * AMB_CONFIG_SIZE + \
55                         AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR)
56 #define AMB_REG_TEMP_MAX(amb)           ((amb) * AMB_CONFIG_SIZE + \
57                         AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR)
58 #define AMB_REG_TEMP(amb)               ((amb) * AMB_CONFIG_SIZE + \
59                         AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR)
60
61 #define MAX_MEM_CHANNELS                4
62 #define MAX_AMBS_PER_CHANNEL            16
63 #define MAX_AMBS                        (MAX_MEM_CHANNELS * \
64                                          MAX_AMBS_PER_CHANNEL)
65 /*
66  * Ugly hack: For some reason the highest bit is set if there
67  * are _any_ DIMMs in the channel.  Attempting to read from
68  * this "high-order" AMB results in a memory bus error, so
69  * for now we'll just ignore that top bit, even though that
70  * might prevent us from seeing the 16th DIMM in the channel.
71  */
72 #define REAL_MAX_AMBS_PER_CHANNEL       15
73 #define KNOBS_PER_AMB                   5
74
75 #define AMB_NUM_FROM_REG(byte_num, bit_num)     ((byte_num) * \
76                         MAX_AMBS_PER_CHANNEL) + (bit_num)
77
78 #define AMB_SYSFS_NAME_LEN              16
79 struct i5k_device_attribute {
80         struct sensor_device_attribute s_attr;
81         char name[AMB_SYSFS_NAME_LEN];
82 };
83
84 struct i5k_amb_data {
85         struct device *hwmon_dev;
86
87         unsigned long amb_base;
88         unsigned long amb_len;
89         u16 amb_present[MAX_MEM_CHANNELS];
90         void __iomem *amb_mmio;
91         struct i5k_device_attribute *attrs;
92         unsigned int num_attrs;
93 };
94
95 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
96                          char *buf)
97 {
98         return sprintf(buf, "%s\n", DRVNAME);
99 }
100
101
102 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
103
104 static struct platform_device *amb_pdev;
105
106 static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
107 {
108         return ioread8(data->amb_mmio + offset);
109 }
110
111 static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
112                            u8 val)
113 {
114         iowrite8(val, data->amb_mmio + offset);
115 }
116
117 static ssize_t show_amb_alarm(struct device *dev,
118                              struct device_attribute *devattr,
119                              char *buf)
120 {
121         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
122         struct i5k_amb_data *data = dev_get_drvdata(dev);
123
124         if (!(amb_read_byte(data, AMB_REG_TEMP_STATUS(attr->index)) & 0x20) &&
125              (amb_read_byte(data, AMB_REG_TEMP_STATUS(attr->index)) & 0x8))
126                 return sprintf(buf, "1\n");
127         else
128                 return sprintf(buf, "0\n");
129 }
130
131 static ssize_t store_amb_min(struct device *dev,
132                              struct device_attribute *devattr,
133                              const char *buf,
134                              size_t count)
135 {
136         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
137         struct i5k_amb_data *data = dev_get_drvdata(dev);
138         unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
139
140         if (temp > 255)
141                 temp = 255;
142
143         amb_write_byte(data, AMB_REG_TEMP_MIN(attr->index), temp);
144         return count;
145 }
146
147 static ssize_t store_amb_mid(struct device *dev,
148                              struct device_attribute *devattr,
149                              const char *buf,
150                              size_t count)
151 {
152         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
153         struct i5k_amb_data *data = dev_get_drvdata(dev);
154         unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
155
156         if (temp > 255)
157                 temp = 255;
158
159         amb_write_byte(data, AMB_REG_TEMP_MID(attr->index), temp);
160         return count;
161 }
162
163 static ssize_t store_amb_max(struct device *dev,
164                              struct device_attribute *devattr,
165                              const char *buf,
166                              size_t count)
167 {
168         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
169         struct i5k_amb_data *data = dev_get_drvdata(dev);
170         unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
171
172         if (temp > 255)
173                 temp = 255;
174
175         amb_write_byte(data, AMB_REG_TEMP_MAX(attr->index), temp);
176         return count;
177 }
178
179 static ssize_t show_amb_min(struct device *dev,
180                              struct device_attribute *devattr,
181                              char *buf)
182 {
183         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184         struct i5k_amb_data *data = dev_get_drvdata(dev);
185         return sprintf(buf, "%d\n",
186                 500 * amb_read_byte(data, AMB_REG_TEMP_MIN(attr->index)));
187 }
188
189 static ssize_t show_amb_mid(struct device *dev,
190                              struct device_attribute *devattr,
191                              char *buf)
192 {
193         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194         struct i5k_amb_data *data = dev_get_drvdata(dev);
195         return sprintf(buf, "%d\n",
196                 500 * amb_read_byte(data, AMB_REG_TEMP_MID(attr->index)));
197 }
198
199 static ssize_t show_amb_max(struct device *dev,
200                              struct device_attribute *devattr,
201                              char *buf)
202 {
203         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
204         struct i5k_amb_data *data = dev_get_drvdata(dev);
205         return sprintf(buf, "%d\n",
206                 500 * amb_read_byte(data, AMB_REG_TEMP_MAX(attr->index)));
207 }
208
209 static ssize_t show_amb_temp(struct device *dev,
210                              struct device_attribute *devattr,
211                              char *buf)
212 {
213         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
214         struct i5k_amb_data *data = dev_get_drvdata(dev);
215         return sprintf(buf, "%d\n",
216                 500 * amb_read_byte(data, AMB_REG_TEMP(attr->index)));
217 }
218
219 static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
220 {
221         int i, j, k, d = 0;
222         u16 c;
223         int res = 0;
224         int num_ambs = 0;
225         struct i5k_amb_data *data = platform_get_drvdata(pdev);
226
227         /* Count the number of AMBs found */
228         /* ignore the high-order bit, see "Ugly hack" comment above */
229         for (i = 0; i < MAX_MEM_CHANNELS; i++)
230                 num_ambs += hweight16(data->amb_present[i] & 0x7fff);
231
232         /* Set up sysfs stuff */
233         data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
234                                 GFP_KERNEL);
235         if (!data->attrs)
236                 return -ENOMEM;
237         data->num_attrs = 0;
238
239         for (i = 0; i < MAX_MEM_CHANNELS; i++) {
240                 c = data->amb_present[i];
241                 for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
242                         struct i5k_device_attribute *iattr;
243
244                         k = AMB_NUM_FROM_REG(i, j);
245                         if (!(c & 0x1))
246                                 continue;
247                         d++;
248
249                         /* Temperature sysfs knob */
250                         iattr = data->attrs + data->num_attrs;
251                         snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
252                                  "temp%d_input", d);
253                         iattr->s_attr.dev_attr.attr.name = iattr->name;
254                         iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
255                         iattr->s_attr.dev_attr.show = show_amb_temp;
256                         iattr->s_attr.index = k;
257                         res = device_create_file(&pdev->dev,
258                                                  &iattr->s_attr.dev_attr);
259                         if (res)
260                                 goto exit_remove;
261                         data->num_attrs++;
262
263                         /* Temperature min sysfs knob */
264                         iattr = data->attrs + data->num_attrs;
265                         snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
266                                  "temp%d_min", d);
267                         iattr->s_attr.dev_attr.attr.name = iattr->name;
268                         iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
269                         iattr->s_attr.dev_attr.show = show_amb_min;
270                         iattr->s_attr.dev_attr.store = store_amb_min;
271                         iattr->s_attr.index = k;
272                         res = device_create_file(&pdev->dev,
273                                                  &iattr->s_attr.dev_attr);
274                         if (res)
275                                 goto exit_remove;
276                         data->num_attrs++;
277
278                         /* Temperature mid sysfs knob */
279                         iattr = data->attrs + data->num_attrs;
280                         snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
281                                  "temp%d_mid", d);
282                         iattr->s_attr.dev_attr.attr.name = iattr->name;
283                         iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
284                         iattr->s_attr.dev_attr.show = show_amb_mid;
285                         iattr->s_attr.dev_attr.store = store_amb_mid;
286                         iattr->s_attr.index = k;
287                         res = device_create_file(&pdev->dev,
288                                                  &iattr->s_attr.dev_attr);
289                         if (res)
290                                 goto exit_remove;
291                         data->num_attrs++;
292
293                         /* Temperature max sysfs knob */
294                         iattr = data->attrs + data->num_attrs;
295                         snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
296                                  "temp%d_max", d);
297                         iattr->s_attr.dev_attr.attr.name = iattr->name;
298                         iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
299                         iattr->s_attr.dev_attr.show = show_amb_max;
300                         iattr->s_attr.dev_attr.store = store_amb_max;
301                         iattr->s_attr.index = k;
302                         res = device_create_file(&pdev->dev,
303                                                  &iattr->s_attr.dev_attr);
304                         if (res)
305                                 goto exit_remove;
306                         data->num_attrs++;
307
308                         /* Temperature alarm sysfs knob */
309                         iattr = data->attrs + data->num_attrs;
310                         snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
311                                  "temp%d_alarm", d);
312                         iattr->s_attr.dev_attr.attr.name = iattr->name;
313                         iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
314                         iattr->s_attr.dev_attr.show = show_amb_alarm;
315                         iattr->s_attr.index = k;
316                         res = device_create_file(&pdev->dev,
317                                                  &iattr->s_attr.dev_attr);
318                         if (res)
319                                 goto exit_remove;
320                         data->num_attrs++;
321                 }
322         }
323
324         res = device_create_file(&pdev->dev, &dev_attr_name);
325         if (res)
326                 goto exit_remove;
327
328         data->hwmon_dev = hwmon_device_register(&pdev->dev);
329         if (IS_ERR(data->hwmon_dev)) {
330                 res = PTR_ERR(data->hwmon_dev);
331                 goto exit_remove;
332         }
333
334         return res;
335
336 exit_remove:
337         device_remove_file(&pdev->dev, &dev_attr_name);
338         for (i = 0; i < data->num_attrs; i++)
339                 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
340         kfree(data->attrs);
341
342         return res;
343 }
344
345 static int __devinit i5k_amb_add(void)
346 {
347         int res = -ENODEV;
348
349         /* only ever going to be one of these */
350         amb_pdev = platform_device_alloc(DRVNAME, 0);
351         if (!amb_pdev)
352                 return -ENOMEM;
353
354         res = platform_device_add(amb_pdev);
355         if (res)
356                 goto err;
357         return 0;
358
359 err:
360         platform_device_put(amb_pdev);
361         return res;
362 }
363
364 static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data)
365 {
366         struct pci_dev *pcidev;
367         u32 val32;
368         int res = -ENODEV;
369
370         /* Find AMB register memory space */
371         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
372                                 PCI_DEVICE_ID_INTEL_5000_ERR,
373                                 NULL);
374         if (!pcidev)
375                 return -ENODEV;
376
377         if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
378                 goto out;
379         data->amb_base = val32;
380
381         if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
382                 goto out;
383         data->amb_len = val32;
384
385         /* Is it big enough? */
386         if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
387                 dev_err(&pcidev->dev, "AMB region too small!\n");
388                 goto out;
389         }
390
391         res = 0;
392 out:
393         pci_dev_put(pcidev);
394         return res;
395 }
396
397 static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
398 {
399         struct pci_dev *pcidev;
400         u16 val16;
401         int res = -ENODEV;
402
403         /* Copy the DIMM presence map for these two channels */
404         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
405         if (!pcidev)
406                 return -ENODEV;
407
408         if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
409                 goto out;
410         amb_present[0] = val16;
411
412         if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
413                 goto out;
414         amb_present[1] = val16;
415
416         res = 0;
417
418 out:
419         pci_dev_put(pcidev);
420         return res;
421 }
422
423 static int __devinit i5k_amb_probe(struct platform_device *pdev)
424 {
425         struct i5k_amb_data *data;
426         struct resource *reso;
427         int res = -ENODEV;
428
429         data = kzalloc(sizeof(*data), GFP_KERNEL);
430         if (!data)
431                 return -ENOMEM;
432
433         /* Figure out where the AMB registers live */
434         res = i5k_find_amb_registers(data);
435         if (res)
436                 goto err;
437
438         /* Copy the DIMM presence map for the first two channels */
439         res = i5k_channel_probe(&data->amb_present[0],
440                                 PCI_DEVICE_ID_INTEL_5000_FBD0);
441         if (res)
442                 goto err;
443
444         /* Copy the DIMM presence map for the optional second two channels */
445         i5k_channel_probe(&data->amb_present[2],
446                           PCI_DEVICE_ID_INTEL_5000_FBD1);
447
448         /* Set up resource regions */
449         reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
450         if (!reso) {
451                 res = -EBUSY;
452                 goto err;
453         }
454
455         data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
456         if (!data->amb_mmio) {
457                 res = -EBUSY;
458                 goto err_map_failed;
459         }
460
461         platform_set_drvdata(pdev, data);
462
463         res = i5k_amb_hwmon_init(pdev);
464         if (res)
465                 goto err_init_failed;
466
467         return res;
468
469 err_init_failed:
470         iounmap(data->amb_mmio);
471         platform_set_drvdata(pdev, NULL);
472 err_map_failed:
473         release_mem_region(data->amb_base, data->amb_len);
474 err:
475         kfree(data);
476         return res;
477 }
478
479 static int __devexit i5k_amb_remove(struct platform_device *pdev)
480 {
481         int i;
482         struct i5k_amb_data *data = platform_get_drvdata(pdev);
483
484         hwmon_device_unregister(data->hwmon_dev);
485         device_remove_file(&pdev->dev, &dev_attr_name);
486         for (i = 0; i < data->num_attrs; i++)
487                 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
488         kfree(data->attrs);
489         iounmap(data->amb_mmio);
490         release_mem_region(data->amb_base, data->amb_len);
491         platform_set_drvdata(pdev, NULL);
492         kfree(data);
493         return 0;
494 }
495
496 static struct platform_driver i5k_amb_driver = {
497         .driver = {
498                 .owner = THIS_MODULE,
499                 .name = DRVNAME,
500         },
501         .probe = i5k_amb_probe,
502         .remove = __devexit_p(i5k_amb_remove),
503 };
504
505 static int __init i5k_amb_init(void)
506 {
507         int res;
508
509         res = platform_driver_register(&i5k_amb_driver);
510         if (res)
511                 return res;
512
513         res = i5k_amb_add();
514         if (res)
515                 platform_driver_unregister(&i5k_amb_driver);
516
517         return res;
518 }
519
520 static void __exit i5k_amb_exit(void)
521 {
522         platform_device_unregister(amb_pdev);
523         platform_driver_unregister(&i5k_amb_driver);
524 }
525
526 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
527 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
528 MODULE_LICENSE("GPL");
529
530 module_init(i5k_amb_init);
531 module_exit(i5k_amb_exit);