blob: 783fa936e4d167e8668b829a7746bf382c10ee6e [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Darrick J. Wong298c7522007-10-18 13:22:43 -07002/*
3 * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
4 * temperature sensors
5 * Copyright (C) 2007 IBM
6 *
Darrick J. Wong5407e0512013-08-26 15:42:27 -07007 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong298c7522007-10-18 13:22:43 -07008 */
9
10#include <linux/module.h>
Darrick J. Wong298c7522007-10-18 13:22:43 -070011#include <linux/hwmon.h>
12#include <linux/hwmon-sysfs.h>
13#include <linux/err.h>
14#include <linux/mutex.h>
Darrick J. Wong298c7522007-10-18 13:22:43 -070015#include <linux/log2.h>
16#include <linux/pci.h>
17#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Darrick J. Wong298c7522007-10-18 13:22:43 -070019
20#define DRVNAME "i5k_amb"
21
22#define I5K_REG_AMB_BASE_ADDR 0x48
23#define I5K_REG_AMB_LEN_ADDR 0x50
24#define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
25#define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
26
27#define AMB_REG_TEMP_MIN_ADDR 0x80
28#define AMB_REG_TEMP_MID_ADDR 0x81
29#define AMB_REG_TEMP_MAX_ADDR 0x82
30#define AMB_REG_TEMP_STATUS_ADDR 0x84
31#define AMB_REG_TEMP_ADDR 0x85
32
33#define AMB_CONFIG_SIZE 2048
34#define AMB_FUNC_3_OFFSET 768
35
Darrick J. Wong59a030a2007-10-26 11:55:11 -070036static unsigned long amb_reg_temp_status(unsigned int amb)
37{
38 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
39 AMB_CONFIG_SIZE * amb;
40}
41
42static unsigned long amb_reg_temp_min(unsigned int amb)
43{
44 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
45 AMB_CONFIG_SIZE * amb;
46}
47
48static unsigned long amb_reg_temp_mid(unsigned int amb)
49{
50 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
51 AMB_CONFIG_SIZE * amb;
52}
53
54static unsigned long amb_reg_temp_max(unsigned int amb)
55{
56 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
57 AMB_CONFIG_SIZE * amb;
58}
59
60static unsigned long amb_reg_temp(unsigned int amb)
61{
62 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
63 AMB_CONFIG_SIZE * amb;
64}
Darrick J. Wong298c7522007-10-18 13:22:43 -070065
66#define MAX_MEM_CHANNELS 4
67#define MAX_AMBS_PER_CHANNEL 16
68#define MAX_AMBS (MAX_MEM_CHANNELS * \
69 MAX_AMBS_PER_CHANNEL)
Darrick J. Wong963d96b2008-08-15 00:40:36 -070070#define CHANNEL_SHIFT 4
71#define DIMM_MASK 0xF
Darrick J. Wong298c7522007-10-18 13:22:43 -070072/*
73 * Ugly hack: For some reason the highest bit is set if there
74 * are _any_ DIMMs in the channel. Attempting to read from
75 * this "high-order" AMB results in a memory bus error, so
76 * for now we'll just ignore that top bit, even though that
77 * might prevent us from seeing the 16th DIMM in the channel.
78 */
79#define REAL_MAX_AMBS_PER_CHANNEL 15
Darrick J. Wong963d96b2008-08-15 00:40:36 -070080#define KNOBS_PER_AMB 6
Darrick J. Wong298c7522007-10-18 13:22:43 -070081
Darrick J. Wong59a030a2007-10-26 11:55:11 -070082static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
83{
84 return byte_num * MAX_AMBS_PER_CHANNEL + bit;
85}
Darrick J. Wong298c7522007-10-18 13:22:43 -070086
87#define AMB_SYSFS_NAME_LEN 16
88struct i5k_device_attribute {
89 struct sensor_device_attribute s_attr;
90 char name[AMB_SYSFS_NAME_LEN];
91};
92
93struct i5k_amb_data {
94 struct device *hwmon_dev;
95
96 unsigned long amb_base;
97 unsigned long amb_len;
98 u16 amb_present[MAX_MEM_CHANNELS];
99 void __iomem *amb_mmio;
100 struct i5k_device_attribute *attrs;
101 unsigned int num_attrs;
102};
103
Julia Lawall3edf03b2016-12-22 13:04:47 +0100104static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong298c7522007-10-18 13:22:43 -0700105 char *buf)
106{
107 return sprintf(buf, "%s\n", DRVNAME);
108}
109
110
Julia Lawall3edf03b2016-12-22 13:04:47 +0100111static DEVICE_ATTR_RO(name);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700112
113static struct platform_device *amb_pdev;
114
115static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
116{
117 return ioread8(data->amb_mmio + offset);
118}
119
120static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
121 u8 val)
122{
123 iowrite8(val, data->amb_mmio + offset);
124}
125
126static ssize_t show_amb_alarm(struct device *dev,
127 struct device_attribute *devattr,
128 char *buf)
129{
130 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
131 struct i5k_amb_data *data = dev_get_drvdata(dev);
132
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700133 if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
134 (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
Darrick J. Wong298c7522007-10-18 13:22:43 -0700135 return sprintf(buf, "1\n");
136 else
137 return sprintf(buf, "0\n");
138}
139
140static ssize_t store_amb_min(struct device *dev,
141 struct device_attribute *devattr,
142 const char *buf,
143 size_t count)
144{
145 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
146 struct i5k_amb_data *data = dev_get_drvdata(dev);
Frans Meulenbroeks7b102ed2012-01-08 19:34:20 +0100147 unsigned long temp;
148 int ret = kstrtoul(buf, 10, &temp);
149 if (ret < 0)
150 return ret;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700151
Frans Meulenbroeks7b102ed2012-01-08 19:34:20 +0100152 temp = temp / 500;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700153 if (temp > 255)
154 temp = 255;
155
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700156 amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700157 return count;
158}
159
160static ssize_t store_amb_mid(struct device *dev,
161 struct device_attribute *devattr,
162 const char *buf,
163 size_t count)
164{
165 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166 struct i5k_amb_data *data = dev_get_drvdata(dev);
Frans Meulenbroeks7b102ed2012-01-08 19:34:20 +0100167 unsigned long temp;
168 int ret = kstrtoul(buf, 10, &temp);
169 if (ret < 0)
170 return ret;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700171
Frans Meulenbroeks7b102ed2012-01-08 19:34:20 +0100172 temp = temp / 500;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700173 if (temp > 255)
174 temp = 255;
175
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700176 amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700177 return count;
178}
179
180static ssize_t store_amb_max(struct device *dev,
181 struct device_attribute *devattr,
182 const char *buf,
183 size_t count)
184{
185 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
186 struct i5k_amb_data *data = dev_get_drvdata(dev);
Frans Meulenbroeks7b102ed2012-01-08 19:34:20 +0100187 unsigned long temp;
188 int ret = kstrtoul(buf, 10, &temp);
189 if (ret < 0)
190 return ret;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700191
Frans Meulenbroeks7b102ed2012-01-08 19:34:20 +0100192 temp = temp / 500;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700193 if (temp > 255)
194 temp = 255;
195
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700196 amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700197 return count;
198}
199
200static ssize_t show_amb_min(struct device *dev,
201 struct device_attribute *devattr,
202 char *buf)
203{
204 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
205 struct i5k_amb_data *data = dev_get_drvdata(dev);
206 return sprintf(buf, "%d\n",
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700207 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
Darrick J. Wong298c7522007-10-18 13:22:43 -0700208}
209
210static ssize_t show_amb_mid(struct device *dev,
211 struct device_attribute *devattr,
212 char *buf)
213{
214 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
215 struct i5k_amb_data *data = dev_get_drvdata(dev);
216 return sprintf(buf, "%d\n",
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700217 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
Darrick J. Wong298c7522007-10-18 13:22:43 -0700218}
219
220static ssize_t show_amb_max(struct device *dev,
221 struct device_attribute *devattr,
222 char *buf)
223{
224 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
225 struct i5k_amb_data *data = dev_get_drvdata(dev);
226 return sprintf(buf, "%d\n",
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700227 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
Darrick J. Wong298c7522007-10-18 13:22:43 -0700228}
229
230static ssize_t show_amb_temp(struct device *dev,
231 struct device_attribute *devattr,
232 char *buf)
233{
234 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
235 struct i5k_amb_data *data = dev_get_drvdata(dev);
236 return sprintf(buf, "%d\n",
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700237 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
Darrick J. Wong298c7522007-10-18 13:22:43 -0700238}
239
Darrick J. Wong963d96b2008-08-15 00:40:36 -0700240static ssize_t show_label(struct device *dev,
241 struct device_attribute *devattr,
242 char *buf)
243{
244 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
245
246 return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
247 attr->index & DIMM_MASK);
248}
249
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500250static int i5k_amb_hwmon_init(struct platform_device *pdev)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700251{
252 int i, j, k, d = 0;
253 u16 c;
254 int res = 0;
255 int num_ambs = 0;
256 struct i5k_amb_data *data = platform_get_drvdata(pdev);
257
258 /* Count the number of AMBs found */
259 /* ignore the high-order bit, see "Ugly hack" comment above */
260 for (i = 0; i < MAX_MEM_CHANNELS; i++)
261 num_ambs += hweight16(data->amb_present[i] & 0x7fff);
262
263 /* Set up sysfs stuff */
Kees Cook6396bb22018-06-12 14:03:40 -0700264 data->attrs = kzalloc(array3_size(num_ambs, KNOBS_PER_AMB,
265 sizeof(*data->attrs)),
266 GFP_KERNEL);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700267 if (!data->attrs)
268 return -ENOMEM;
269 data->num_attrs = 0;
270
271 for (i = 0; i < MAX_MEM_CHANNELS; i++) {
272 c = data->amb_present[i];
273 for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
274 struct i5k_device_attribute *iattr;
275
Darrick J. Wong59a030a2007-10-26 11:55:11 -0700276 k = amb_num_from_reg(i, j);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700277 if (!(c & 0x1))
278 continue;
279 d++;
280
Darrick J. Wong963d96b2008-08-15 00:40:36 -0700281 /* sysfs label */
282 iattr = data->attrs + data->num_attrs;
283 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
284 "temp%d_label", d);
285 iattr->s_attr.dev_attr.attr.name = iattr->name;
Guenter Roeck2f8f7102018-12-10 14:02:08 -0800286 iattr->s_attr.dev_attr.attr.mode = 0444;
Darrick J. Wong963d96b2008-08-15 00:40:36 -0700287 iattr->s_attr.dev_attr.show = show_label;
288 iattr->s_attr.index = k;
KAMEZAWA Hiroyuki0e6c7872010-06-20 09:22:31 +0200289 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
Darrick J. Wong963d96b2008-08-15 00:40:36 -0700290 res = device_create_file(&pdev->dev,
291 &iattr->s_attr.dev_attr);
292 if (res)
293 goto exit_remove;
294 data->num_attrs++;
295
Darrick J. Wong298c7522007-10-18 13:22:43 -0700296 /* Temperature sysfs knob */
297 iattr = data->attrs + data->num_attrs;
298 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
299 "temp%d_input", d);
300 iattr->s_attr.dev_attr.attr.name = iattr->name;
Guenter Roeck2f8f7102018-12-10 14:02:08 -0800301 iattr->s_attr.dev_attr.attr.mode = 0444;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700302 iattr->s_attr.dev_attr.show = show_amb_temp;
303 iattr->s_attr.index = k;
KAMEZAWA Hiroyuki0e6c7872010-06-20 09:22:31 +0200304 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700305 res = device_create_file(&pdev->dev,
306 &iattr->s_attr.dev_attr);
307 if (res)
308 goto exit_remove;
309 data->num_attrs++;
310
311 /* Temperature min sysfs knob */
312 iattr = data->attrs + data->num_attrs;
313 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
314 "temp%d_min", d);
315 iattr->s_attr.dev_attr.attr.name = iattr->name;
Guenter Roeck2f8f7102018-12-10 14:02:08 -0800316 iattr->s_attr.dev_attr.attr.mode = 0644;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700317 iattr->s_attr.dev_attr.show = show_amb_min;
318 iattr->s_attr.dev_attr.store = store_amb_min;
319 iattr->s_attr.index = k;
KAMEZAWA Hiroyuki0e6c7872010-06-20 09:22:31 +0200320 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700321 res = device_create_file(&pdev->dev,
322 &iattr->s_attr.dev_attr);
323 if (res)
324 goto exit_remove;
325 data->num_attrs++;
326
327 /* Temperature mid sysfs knob */
328 iattr = data->attrs + data->num_attrs;
329 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
330 "temp%d_mid", d);
331 iattr->s_attr.dev_attr.attr.name = iattr->name;
Guenter Roeck2f8f7102018-12-10 14:02:08 -0800332 iattr->s_attr.dev_attr.attr.mode = 0644;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700333 iattr->s_attr.dev_attr.show = show_amb_mid;
334 iattr->s_attr.dev_attr.store = store_amb_mid;
335 iattr->s_attr.index = k;
KAMEZAWA Hiroyuki0e6c7872010-06-20 09:22:31 +0200336 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700337 res = device_create_file(&pdev->dev,
338 &iattr->s_attr.dev_attr);
339 if (res)
340 goto exit_remove;
341 data->num_attrs++;
342
343 /* Temperature max sysfs knob */
344 iattr = data->attrs + data->num_attrs;
345 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
346 "temp%d_max", d);
347 iattr->s_attr.dev_attr.attr.name = iattr->name;
Guenter Roeck2f8f7102018-12-10 14:02:08 -0800348 iattr->s_attr.dev_attr.attr.mode = 0644;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700349 iattr->s_attr.dev_attr.show = show_amb_max;
350 iattr->s_attr.dev_attr.store = store_amb_max;
351 iattr->s_attr.index = k;
KAMEZAWA Hiroyuki0e6c7872010-06-20 09:22:31 +0200352 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700353 res = device_create_file(&pdev->dev,
354 &iattr->s_attr.dev_attr);
355 if (res)
356 goto exit_remove;
357 data->num_attrs++;
358
359 /* Temperature alarm sysfs knob */
360 iattr = data->attrs + data->num_attrs;
361 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
362 "temp%d_alarm", d);
363 iattr->s_attr.dev_attr.attr.name = iattr->name;
Guenter Roeck2f8f7102018-12-10 14:02:08 -0800364 iattr->s_attr.dev_attr.attr.mode = 0444;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700365 iattr->s_attr.dev_attr.show = show_amb_alarm;
366 iattr->s_attr.index = k;
KAMEZAWA Hiroyuki0e6c7872010-06-20 09:22:31 +0200367 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700368 res = device_create_file(&pdev->dev,
369 &iattr->s_attr.dev_attr);
370 if (res)
371 goto exit_remove;
372 data->num_attrs++;
373 }
374 }
375
376 res = device_create_file(&pdev->dev, &dev_attr_name);
377 if (res)
378 goto exit_remove;
379
380 data->hwmon_dev = hwmon_device_register(&pdev->dev);
381 if (IS_ERR(data->hwmon_dev)) {
382 res = PTR_ERR(data->hwmon_dev);
383 goto exit_remove;
384 }
385
386 return res;
387
388exit_remove:
389 device_remove_file(&pdev->dev, &dev_attr_name);
390 for (i = 0; i < data->num_attrs; i++)
391 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
392 kfree(data->attrs);
393
394 return res;
395}
396
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500397static int i5k_amb_add(void)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700398{
Colin Ian King9e444232020-06-10 13:36:38 +0100399 int res;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700400
401 /* only ever going to be one of these */
402 amb_pdev = platform_device_alloc(DRVNAME, 0);
403 if (!amb_pdev)
404 return -ENOMEM;
405
406 res = platform_device_add(amb_pdev);
407 if (res)
408 goto err;
409 return 0;
410
411err:
412 platform_device_put(amb_pdev);
413 return res;
414}
415
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500416static int i5k_find_amb_registers(struct i5k_amb_data *data,
Darrick J. Wongb8fdaf52008-05-23 13:04:25 -0700417 unsigned long devid)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700418{
419 struct pci_dev *pcidev;
420 u32 val32;
421 int res = -ENODEV;
422
423 /* Find AMB register memory space */
424 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
Darrick J. Wongb8fdaf52008-05-23 13:04:25 -0700425 devid,
Darrick J. Wong298c7522007-10-18 13:22:43 -0700426 NULL);
427 if (!pcidev)
428 return -ENODEV;
429
Saheed O. Bolarinwa22075152020-08-01 13:24:39 +0200430 pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32);
431 if (val32 == (u32)~0)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700432 goto out;
433 data->amb_base = val32;
434
Saheed O. Bolarinwa22075152020-08-01 13:24:39 +0200435 pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32);
436 if (val32 == (u32)~0)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700437 goto out;
438 data->amb_len = val32;
439
440 /* Is it big enough? */
441 if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
442 dev_err(&pcidev->dev, "AMB region too small!\n");
443 goto out;
444 }
445
446 res = 0;
447out:
448 pci_dev_put(pcidev);
449 return res;
450}
451
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500452static int i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700453{
454 struct pci_dev *pcidev;
455 u16 val16;
456 int res = -ENODEV;
457
458 /* Copy the DIMM presence map for these two channels */
459 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
460 if (!pcidev)
461 return -ENODEV;
462
Saheed O. Bolarinwa22075152020-08-01 13:24:39 +0200463 pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16);
464 if (val16 == (u16)~0)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700465 goto out;
466 amb_present[0] = val16;
467
Saheed O. Bolarinwa22075152020-08-01 13:24:39 +0200468 pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16);
469 if (val16 == (u16)~0)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700470 goto out;
471 amb_present[1] = val16;
472
473 res = 0;
474
475out:
476 pci_dev_put(pcidev);
477 return res;
478}
479
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700480static struct {
481 unsigned long err;
482 unsigned long fbd0;
Bill Pembertona5977242012-11-19 13:24:37 -0500483} chipset_ids[] = {
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700484 { PCI_DEVICE_ID_INTEL_5000_ERR, PCI_DEVICE_ID_INTEL_5000_FBD0 },
485 { PCI_DEVICE_ID_INTEL_5400_ERR, PCI_DEVICE_ID_INTEL_5400_FBD0 },
486 { 0, 0 }
Darrick J. Wongb8fdaf52008-05-23 13:04:25 -0700487};
488
Guenter Roeckc48a2912010-11-19 09:39:11 -0800489#ifdef MODULE
Arvind Yadav4d3f2412017-07-17 21:55:10 +0530490static const struct pci_device_id i5k_amb_ids[] = {
Darrick J. Wong6e31eb22009-01-07 16:37:33 +0100491 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5000_ERR) },
492 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5400_ERR) },
493 { 0, }
494};
495MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
Guenter Roeckc48a2912010-11-19 09:39:11 -0800496#endif
Darrick J. Wong6e31eb22009-01-07 16:37:33 +0100497
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500498static int i5k_amb_probe(struct platform_device *pdev)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700499{
500 struct i5k_amb_data *data;
501 struct resource *reso;
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700502 int i, res;
Darrick J. Wong298c7522007-10-18 13:22:43 -0700503
504 data = kzalloc(sizeof(*data), GFP_KERNEL);
505 if (!data)
506 return -ENOMEM;
507
508 /* Figure out where the AMB registers live */
Darrick J. Wongb8fdaf52008-05-23 13:04:25 -0700509 i = 0;
510 do {
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700511 res = i5k_find_amb_registers(data, chipset_ids[i].err);
512 if (res == 0)
513 break;
Darrick J. Wongb8fdaf52008-05-23 13:04:25 -0700514 i++;
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700515 } while (chipset_ids[i].err);
Darrick J. Wongb8fdaf52008-05-23 13:04:25 -0700516
Darrick J. Wong298c7522007-10-18 13:22:43 -0700517 if (res)
518 goto err;
519
520 /* Copy the DIMM presence map for the first two channels */
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700521 res = i5k_channel_probe(&data->amb_present[0], chipset_ids[i].fbd0);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700522 if (res)
523 goto err;
524
525 /* Copy the DIMM presence map for the optional second two channels */
Jean Delvareb4cb0d42011-08-23 21:04:28 -0700526 i5k_channel_probe(&data->amb_present[2], chipset_ids[i].fbd0 + 1);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700527
528 /* Set up resource regions */
529 reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
530 if (!reso) {
531 res = -EBUSY;
532 goto err;
533 }
534
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100535 data->amb_mmio = ioremap(data->amb_base, data->amb_len);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700536 if (!data->amb_mmio) {
537 res = -EBUSY;
538 goto err_map_failed;
539 }
540
541 platform_set_drvdata(pdev, data);
542
543 res = i5k_amb_hwmon_init(pdev);
544 if (res)
545 goto err_init_failed;
546
547 return res;
548
549err_init_failed:
550 iounmap(data->amb_mmio);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700551err_map_failed:
552 release_mem_region(data->amb_base, data->amb_len);
553err:
554 kfree(data);
555 return res;
556}
557
Bill Pemberton281dfd02012-11-19 13:25:51 -0500558static int i5k_amb_remove(struct platform_device *pdev)
Darrick J. Wong298c7522007-10-18 13:22:43 -0700559{
560 int i;
561 struct i5k_amb_data *data = platform_get_drvdata(pdev);
562
563 hwmon_device_unregister(data->hwmon_dev);
564 device_remove_file(&pdev->dev, &dev_attr_name);
565 for (i = 0; i < data->num_attrs; i++)
566 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
567 kfree(data->attrs);
568 iounmap(data->amb_mmio);
569 release_mem_region(data->amb_base, data->amb_len);
Darrick J. Wong298c7522007-10-18 13:22:43 -0700570 kfree(data);
571 return 0;
572}
573
574static struct platform_driver i5k_amb_driver = {
575 .driver = {
Darrick J. Wong298c7522007-10-18 13:22:43 -0700576 .name = DRVNAME,
577 },
578 .probe = i5k_amb_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500579 .remove = i5k_amb_remove,
Darrick J. Wong298c7522007-10-18 13:22:43 -0700580};
581
582static int __init i5k_amb_init(void)
583{
584 int res;
585
586 res = platform_driver_register(&i5k_amb_driver);
587 if (res)
588 return res;
589
590 res = i5k_amb_add();
591 if (res)
592 platform_driver_unregister(&i5k_amb_driver);
593
594 return res;
595}
596
597static void __exit i5k_amb_exit(void)
598{
599 platform_device_unregister(amb_pdev);
600 platform_driver_unregister(&i5k_amb_driver);
601}
602
Darrick J. Wong5407e0512013-08-26 15:42:27 -0700603MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
Darrick J. Wong298c7522007-10-18 13:22:43 -0700604MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
605MODULE_LICENSE("GPL");
606
607module_init(i5k_amb_init);
608module_exit(i5k_amb_exit);